The Control Class

ASP.NET server-side controls derive from a class named System.Web.UI.Control. In fact, the Control class is the core of almost every User Interface element within ASP.NET. Even System.Web.UI.Page is derived from the Control class. Listing 4-1 shows a small sampling of the System.Web.UI.Page class.

Listing 4-1

public class Control : IComponent, IParserAccessor, …
  public virtual void ApplyStyleSheetSkin();
  protected virtual void CreateChildControls();
  protected virtual void Render(HtmlTextWriter);
  public virtual void RenderControl(HtmlTextWriter);
  protected internal virtual void RenderChildren(HtmlTextWriter);

   public virtual bool Visible {get; set;}
  public virtual bool EnableViewState {get; set;}
  public virtual string SkinID {get; set;}
  public virtual string UniqueID {get;}
  public virtual ControlCollection Controls {get;}

  public virtual Page Page {get; set;}
  public virtual Control Parent {get;}
  protected virtual HttpContext Context {get;}

  public event EventHandler Init;
  public event EventHandler Load;
  public event EventHandler PreRender;
  public event EventHandler Unload;

  internal virtual void OnInit();
  internal virtual void OnLoad();
  internal virtual void OnPreRender();
  internal virtual void OnUnload();

The code in Listing 4-1 shows a small cross section of the functionality available within System.Web.UI.Control. However, it's enough to get an understanding of the class's importance within ASP.NET Web forms. Remember from the last tutorial that ASP.NET Web forms manage a list of controls as part of their internal structure. As you add controls to a Web page, they're placed within the list. When it comes time for a page to render its content back to the client, System.Web.UI.Page walks the list of controls and asks each one of them to render. You can see the Render method in Listing 4-1. Render takes a single argument of type HtmlTextWriter. We'll examine that class later in this tutorial. Right now think of it as the conduit through which you send the page's response back to the client.

Other elements of the Control class include items such as

  • Properties for managing the control's view state

  • Properties for managing skins (to accommodate a consistent look and feel across multiple pages within a site)

  • Properties for getting the parent control (in the case of composite controls) and the parent page

  • Event handlers for the Init, Load, PreRender, and Unload events

  • Methods for raising the Init, Load, PreRender, and Unload events

  • Methods for managing child controls

We'll visit the most important topics in examining both rendered controls and composite controls. The easiest way to start is to jump into building a custom control.