It occurred to me that ASP.Net has been a topic missing from my blog, so to rectify the situation I'm posting three pieces on Controls in the ASP.Net space. Took me ages to get this straight in my own head so with any luck it will make sense here.
Part 1: Web User Controls (ASCX files), the low hanging fruit
What better place to start. These are the odd one out in the ASP.Net control space for a couple of reasons.
- While it is possible, they don't do reuse between projects well.
- They do have a drag and drop design time experience!
- You drag them onto a page from the Solution Explorer, not the toolbox.
First thing to note is they have their own life cycle after Page_Load, and it feels a little like Server.Execute()
While they do have their own code behind class that inherits from System.Web.UI.UserControl, I try to not use too many properties on the class because I feel it gives a misleading impression in implementation. Just say the thing has a text() property and in my Page_Load I set the text to something, well after Page_Load the thing can do what ever it wants to and then I have to go looking for what happened to my value for the text() property.
Re-use between projects requires some planning because they are not seperate from your web project, but re-use inside the same project is what they are best at. Things like a navigational device, a customer lookup widget or something that makes an appearance more than once are good examples. Pull them out, glue them together with a Web User Control, drop them back in. Easy.