Partial Classes and usercontrols

Hi All,

I’ve been looking into creating a partial class for a usercontrol but without luck. The ascx.cs class sees the method in the partial class and the partial class can see methods in the ascx.cs file (via intellisense) but when I do a build it says e.g. CallPartialMethod does not exist in the current context.

I’ve added some quick sample code below to show what I mean.

e.g.

test.ascx

// standard usercontrol

…html

<asp:label id="lblTestLabel" runat="server">Welcome to my test</asp:label>

..html

test.ascx.cs (pointed to by codefile attribute in ascx)

public partial class MyTestUserControl : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

// Works and shows up in intellisense

string test = lblTestLabel.Text;

string test2 = CallPartialMethod();

}

}

partialtest.cs

public partial class MyTestUserControl

{

public string CallPartialMethod()

{

// lblTestLabel shows up as a field (of the proper type) but I have to cast it in order to access the
// .Text Property (well it doesn’t show it via intellisense. I’ve tried to see if it would still build without
// casting but receive the error lblTestLabel does not exist in the current context).

return lblTestLabel.Text;

}

}

 

It’s as if Visual Studio sees the methods etc as that is how partial classes are supposed to work but they do not get included when it comes to building usercontrols (And most probably pages). I tried pointing the codefile setting to partialtest.cs (Had to make the class inherit from usercontrol) and it built fine (Since it didn’t have any references to test.ascx.cs and it isn’t calling any methods from that class) but it still doesn’t include more than one cs file.

Has anyone achieved partial classes with usercontrols or come across this problem before

Thanks,

John



Answer this question

Partial Classes and usercontrols

  • Rohland

    What I think is going on here is you are running into an issue of how ASP.NET compiles a solution. If you watch Fritz Onions webcast http://msevents.microsoft.com/cui/WebCastEventDetails.aspx EventID=1032278422&EventCategory=5&culture=en-US&CountryCode=US he talks about how the compilation model has changed and how things get compiled. The long and the short is that ASP.NET has one partial class that you don't see. This class actually has the lablel declared in it. This is why you no longer have to have a variable declared in your code behind for each control you want to work with. When you put your code into the partial class that Visual Studio creates you are going to have things match up correctly because the ASP.NET compiler joins this hidden partial class that you don't see with the partial class that is exposed to your via Visual Studio. So in your case your test.asxc.cs file is joined with a hidden class and the two work fine. When you add the partialtest.cs class you are probably adding it in App_Code which means that this .cs file will be compiled in another dll. This is why it won't work. Before you respond watch the webcast and I think it will become clearer.



  • Partial Classes and usercontrols