On dynamically creating server controls

Posted by: the telerik blogs, on 28 Sep 2007 | View original | Bookmarked: 0 time(s)

Every once in a while I see code which looks like that:












There are two problems with this code:

  1. The child control is instantiated in the Render method
    From control execution lifecycles point of view it is too late to instantiate controls in the Render method. Even if you add them in the controls collection somefeatures wont work just because its too late postbacks, viewstate to name a few.
  2. The child control is never added to the controls collection. This means the following:
  • Lots of the properties of that child control will be null: Page, Parent etc.
  • A control which is not a child of the the page does not participate of the control execution lifecycle no one calls its OnInit, LoadViewState, SaveViewState, OnPreRender and the rest of the lifecycle supporting methods. As a result most of the features probably wont work saving and loading viewstate, postbacks. Heck, there is a good chance that the control wont work at all. Take the LinkButton for example it just renders its text if you use it in the aforementioned way. Nothing (ok, almost) in the world would make it postback unless you make it part of the controls collection of the page.
  • Web Resources won't work. They require the Page property of the control to be valid. Take the TreeView control for example it will fail with a NullReferenceException in the aforementioned scenario.

If this is not the correct way to instantiate child server controls in composite controls what is?
It is always a good idea to instantiate child server controls inside the CreateChildControls method of your composite control (kudos to the MS guys for such a descriptive name). If you need to instantiate  server controls in a page or user control you can still use CreateChildControls or the Init / Load events. Here is the aforementioned control done in a better way:



CreateChildControls()     {




Controls.Add(someControl);



If you plan to add more than one instance of your custom control in your page it is a good idea to implement the INamingContainer interface. That interface will tell the ASP.NET runtime to generate a unique ID for your controls by prefixing their ID's with the ID of its parent. Fortunately INamingContainer is a marker interface which means you don't have to write any code. You can alternatively inherit from CompositeControl (which also implements INamingContainer).

INamingContainer




or

CompositeControl




How about exposing properties of the child control? You need to make sure you call the EnsureChildControls method before you access the child control instance. Never call CreateChildControls directly - otherwise the child controls will be added more than once. Here is the modified example:








EnsureChildControls(); //Make sure CreateChildControls is called.  



















I highly recommend some additional reading - the excellent series of articles TRULY Understanding Dynamic Controls

Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 874 | Hits: 49

Similar Posts

  • Adding users to a TFS project when youre not on the domain more
  • IIS Media Services 3.0 more
  • An alternative to Crystal more
  • Take Two: A jQuery WCF/ASMX ServiceProxy Client more
  • Using Microsoft's Chart Controls In An ASP.NET Application: Rendering the Chart more
  • Understanding Reverse Proxy Servers and the Mailman more
  • Session State Not Working? Check Your Web Garden! more
  • Installing the FTP Service for IIS 6.0 On An Alternate Port Number and Configuring Windows Firewall more
  • Creating a quick and dirty jQuery Editable Element more
  • Image Transforms with the ASP.NET Generated Image Control more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD