Usercontrols and Partial Classes - Update

Update: See End

Hi All,

I generally prefer to post interesting links/news or tips on this blog but on occassion a question does come out that conventional searching does not solve. 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. I also tried making the partialtest.cs file inherit from usercontrol (and have code file point to test.ascx.cs) and that still gave the same context error.

Has anyone achieved partial classes with usercontrols or come across this problem before (There's a free gmail account in it for you :D Yes I know ......so 2004 ;-) ) ?

Thanks,

John

Update (09/01/2006)

I think it is currently not possible to have additional partial classes with usercontrols using the default website setup in VS2005. I tried various things to no avail. There is one solution:

  • The Web Application project allows you to have additional partial classes for your usercontrols (as all the files are compiled into one assembly). Unfortunately this is still in the very early stages and you end up with a larger single dll (Which may not be what you want).

You could also possibly do it via an MSBuild file (Or Nant).

I tried the deployment project but that didn't seem to work (it still complained when trying to build).

Why are partial classes useful for usercontrols?

Well they can be very useful when you are using code generation.

I'm currently playing with the idea of generating usercontrols with some default code (which would go in the partial class) which would allow the developer to add additional methods etc to the standard codebehind file if required without the risk of losing his/her changes. 

I've ended up creating a protected region that gets preserved across code generations (not the way I would have preferred but it will do for now and it's easy to work with). 

I think you may have been able to have all usercontrols compiled into a single dll per directory (I came across old articles that mentioned it) but it appears this only applies to aspx pages now (January 2006 article): http://msdn.microsoft.com/msdnmag/issues/06/01/ExtremeASPNET/default.aspx?fig=true#fig6

It's a shame you can't simply add an additional partial class to ascx.cs and aspx.cs files out of the box as it would have been a useful feature (Something to look forward to I guess).

If you do come across a good way of achieving this please let me know.

Thanks

John

1 Comment

  • I have exactly the same problem now. I generate ascx and would like to add some custom code that wouldn't be overwritten when generating code again...

    Did you figured out how to do so ?

Comments have been disabled for this content.