The Control class defines properties, methods, and
events that all server controls in ASP.NET require. ASP.NET controls
do not inherit directly from this class; instead, they inherit from
either the System.Web.UI.HtmlControls.HtmlControl
or the System.Web.UI.WebControls.WebControl class,
both of which inherit from the Control class. Similarly, the
Page and UserControl classes
inherit from TemplateControl, which inherits from
this class.
The Control class contains many of the typical
members you would expect in a control, including properties that
reference the root of the control tree (Page) and
a collection of contained controls (Controls). The
EnableViewState property determines whether
ASP.NET maintains the control's state automatically
by using a hidden field. The ViewState property
provides the StateBag collection of state
information.
Most Control methods are used transparently by the
ASP.NET framework, such as Render( ), which
generates the HTML output of a control, and LoadViewState(
) and SaveViewState( ), which manage
view state automatically. One interesting method is
DataBind( ), which binds controls to arrays or
data tables. Third party controls can also extend these methods.
You can inherit from the Control class to create a
simple ASP.NET control. Override the Render( )
method so that the control can generate its own output by using the
supplied HtmlTextWriter. If you are creating a
composite control, you must also override the
CreateChildControls( ) method. Use this method to
instantiate new server-based ASP.NET controls or
LiteralControl objects and add them to the
collection of child controls by using the Add( )
method. If you need to access child controls from another procedure,
and you are not sure if they have been created yet, you can use the
EnsureChildControls( ) method, which automatically
calls CreateChildControls( ), if needed. You
should also implement the INamingContainer
interface to ensure that child controls are created in a distinct
namespace.
Usually, it is easier to derive from
System.Web.UI.WebControls.WebControl when creating
a custom control, as this class provides basic style and color
options and manages the state for these properties automatically. If
your control does not provide user interface, you may want to derive
from the more basic Control class.
public class Control : System.ComponentModel.IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor {
// Public Constructors
public Control( );
// Public Instance Properties
public Control BindingContainer{get; }
public virtual string ClientID{get; }
public virtual ControlCollection Controls{get; }
public virtual bool EnableViewState{set; get; }
public virtual string ID{set; get; }
public virtual Control NamingContainer{get; }
public virtual Page Page{set; get; }
public virtual Control Parent{get; }
public ISite Site{set; get; } // implements System.ComponentModel.IComponent
public virtual string TemplateSourceDirectory{get; }
public virtual string UniqueID{get; }
public virtual bool Visible{set; get; }
// Protected Instance Properties
protected bool ChildControlsCreated{set; get; }
protected virtual HttpContext Context{get; }
protected EventHandlerList Events{get; }
protected bool HasChildViewState{get; }
protected bool IsTrackingViewState{get; }
protected virtual StateBag ViewState{get; }
protected virtual bool ViewStateIgnoresCase{get; }
// Public Instance Methods
public virtual void DataBind( );
public virtual void Dispose( ); // implements IDisposable
public virtual Control FindControl(string id);
public virtual bool HasControls( );
public void RenderControl(HtmlTextWriter writer);
public string ResolveUrl(string relativeUrl);
public void SetRenderMethodDelegate(RenderMethod renderMethod);
// Protected Instance Methods
protected internal virtual void AddedControl(Control control, int index);
protected virtual void AddParsedSubObject(object obj); // implements IParserAccessor
protected void BuildProfileTree(string parentId, bool calcViewState);
protected void ClearChildViewState( );
protected virtual void CreateChildControls( );
protected virtual ControlCollection CreateControlCollection( );
protected virtual void EnsureChildControls( );
protected virtual Control FindControl(string id, int pathOffset);
protected bool IsLiteralContent( );
protected virtual void LoadViewState(object savedState);
protected string MapPathSecure(string virtualPath);
protected virtual bool OnBubbleEvent(object source, EventArgs args);
protected virtual void OnDataBinding(EventArgs e);
protected virtual void OnInit(EventArgs e);
protected virtual void OnLoad(EventArgs e);
protected virtual void OnPreRender(EventArgs e);
protected virtual void OnUnload(EventArgs e);
protected void RaiseBubbleEvent(object source, EventArgs args);
protected internal virtual void RemovedControl(Control control);
protected virtual void Render(HtmlTextWriter writer);
protected virtual void RenderChildren(HtmlTextWriter writer);
protected virtual object SaveViewState( );
protected virtual void TrackViewState( );
// Events
public event EventHandler DataBinding;
public event EventHandler Disposed; // implements System.ComponentModel.IComponent
public event EventHandler Init;
public event EventHandler Load;
public event EventHandler PreRender;
public event EventHandler Unload;
}