CompositeControl Base Class in ASP.NET 2.0 (a new alternative to UserControls)
I stumbled across an interesting blog post on Ian Blackburn's blog tonight. On it he shows a simple sample of how to use the new CompositeControl base class that is provided with ASP.NET 2.0. CompositeControl dramatically simplifies the work needed to create a re-usable ASP.NET custom control.
Ian raises the interesting question of whether this is now a better option to use instead of UserControls (.ascx files) for building re-usable controls for your projects. A couple of advantages they bring:
- The ability to define UI templates (for example: like the <ItemTemplate> property of a Repeater or DataList)
- The ability to provide richer design-time support (wizards, property editors, smart-tasks, etc)
- The ability to add to the VS Toolbox
- The ability to package up and re-use without having to copy the .ascx into a project, as well as to embed resources (for example: images) inside the control assembly
They do require a little more work (and don't have a WYSIWYG designer like usercontrols do), but the above advantages are compelling. One other built-in feature in VS 2005 and Visual Web Developer is that .aspx intellisense for controls (both compiled controls and user-controls) is now automatically generated for you -- so as a control developer you no longer have to jump through hoops to enable this (instead the editor can provide this automatically through reflection).
Here are a few good tutorials I found on the web that discuss how to use the CompositeControl approach more if you are interested in trying it out:
- http://www.codeproject.com/aspnet/customdialog.asp
- http://dotnet.sys-con.com/read/171169.htm
- http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositecontrol.aspx
Hope this helps,
Scott
P.S. Note that UserConrols in VS 2005 and Visual Web Developer *do* now support showing up in WYSIWYG mode instead of being the ugly grey box they were in VS 2003. So that is one big gain that UserControls picked up in VS 2005 (just to help make the debate of which to use harder <g>).