Rough Notes On Web Controls

Just to get it clear in my head, i though id write a few rough notes on creation of web controls. There are three types of web controls in ASP.NET 2.0:

1. Custom Rendered Controls
These derive from the System.Web.UI.WebControls class. To create a rendered control, you simply override the Render method and generate output using Writer.Write(text) where text contains html and writer is of type HtmlTextWriter (passed to the render method).

Note that instead of writing html using Writer.Write(), it is often recommended that you use the inbuilt HtmlTextWriter methods. My personal preference, however, is not to use these – as I like to actually see the html in the code. The benefit of using the HtmlTextWriter is apparently that it includes support to hide the facets of switching between different DTDs (eg html 3.2 and 4.0).

The downside to Custom Rendered Controls is that you cannot reference other asp controls method. If you wish to do this, you will need to create a Composite Control.

2. Composite Controls
Composite controls are composed of other controls. Composite Controls derive from the System.Web.UI.WebControls.CompositeControl class (although they still compile if they inherit just from System.Web.UI.WebControls – I believe the CompositeControl class just effectively adds the INamingContainer interface to the derivation list.

Instead of overriding the render method, you override the CreateChildControls method. Controls are added to this as follows:

Label lblMessage = new Label();
lblMessage.Text = "Hello World";

Note that you still can add custom html directly – e.g. this.Controls.Add(new LiteralControl(“Hello World“));

3. User Controls

Essentially, users controls can be thought of like aspx forms, although instead of forming a page, they form a control that can be tracked ontol the page. Derives from the System.Web.UI.UserControls class. Note that the user control class must exist in a .ascx file, otherwise compilation may fail. When creating a user control in VS2005, you can drag and drop other controls onto it as if you were creating a normal aspx form.

Dynamically adding controls to a composite control
Whereas you can dynamically add controls to a composite control based on events, due to the execution order of controls you cannot wire up that control to events – see

Note that to some extent, you can simulate dynamic adding of controls by setting the visiblity of a control to false when initially rendering.

* Edit
The only way around this is: on the event that creates the dynamic control(s), set up a flag in viewstate (to indicate that the new control(s) are to be created) and whenever the page_load / createchildcontrols runs (after the event to create controls has initially run) call the event to create the dynamic control(s) (making the call from page_load / createchildcontrols). Note that you probably do not want to dynamically call the actual event (due to having to pass args) so instead set up a function that is call both by the event and by page_load / createchildcontrols.
Note that when an event is raised (on an aspx page, I havent actually tried this on a control) the running order is on_init, initialise_component, page_load, then the event being fired. You therefore cannot simply set the viewstate flag in the event and then subsequently generate the control on page load (as the flag to show the dynamically created control will not be set).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>