John Mandia's Points of Interest

Learning is a never-ending adventure :D

News

.Net 2

Categories

Other Sites

Rainbow Related Links

Tools

Unit Testing / TDD

Useful Sites

January 2006 - Posts

Enterprise Library 2.0 For .Net 2.0 Released - Get it while it's Hot!
Hi All,

For those of  you who were waiting for this release you can now grab it from the following url:

http://www.microsoft.com/downloads/details.aspx?FamilyID=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang=en


Enjoy!

John
Posted: Jan 20 2006, 10:09 PM by John_Mandia | with no comments
Filed under:
Update to Partial Classes and UserControls Question

After digging around and finding a solution I decided to post it as an update to the original post in case other people come across the same problem:

http://weblogs.asp.net/jmandia/archive/2006/01/05/434575.aspx

John

Posted: Jan 09 2006, 06:10 PM by John_Mandia | with no comments
Filed under:
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

Posted: Jan 05 2006, 10:10 AM by John_Mandia | with 1 comment(s)
Filed under:
Nice Xml Control

Oleg Tkachenko released a .Net 2 xml control in December which offers a nice number of features over the xml control that comes out of the box:

http://www.xmllab.net/Downloads/tabid/61/Default.aspx

Well worth checking out.

John

Posted: Jan 03 2006, 12:33 PM by John_Mandia | with 1 comment(s)
Filed under:
More Posts