Wednesday, June 20, 2007

Composite Controls in .NET 2.0

These days I'm playing developer, but have my architecture hat close by in the remote chance that I'll get to try it on just even to see if it still fits.

Current interest is composite controls in ASP.NET for the 2.0 framework. I've written a control that combines a textbox with an imagebutton needed for the application that I'm working on. The idea is the user would type some (or no) text in the textbox and then hit the search button that would do a partial search or bring up a dialog for more search criteria depending on the properties set. The reason I turned this into a control, is that it is used all over the application and you know all about architects and consistency....

In experimenting I've found that it is easier to declare the controls as member variables and then instantiate them in the constructor. As properties are set (from parsing the aspx page) or from setting the properties in the class file, it is easy to set the corresponding property of the member variable. Then when you get to the code for CreateChildControls all you have to do is Controls.Clear() and then add your controls without having to set a bunch of properties.

Every example I've seen so far sets all the properties within the CreateChildControls code and this seems a little to heavy handed to me.

Right now in my mind, I'm not sure if there is a downside to doing it my way. Maybe the only thing I can think of is that it violates the aquire late/release early mantra. I'll have to do some testing before I'm completely satisfied.

No comments: