The runat attribute is the key to programming ASP. NET controls. If the label in the aspx Source Code does not contain the runat attribute during declaration, it is regarded as plain text and output word by word. Otherwise, the content of the tag is mapped to the server control and processed in the lifecycle of the page.
Two types of server controls
HTML server controls: HTML controls are mapped to HTML tags through server-side classes. These programming interfaces faithfully represent the attribute set of corresponding htmp tags.
Web server controls: Web controls are more abstract, and their APIs do not strictly follow the HTML syntax. In terms of functionality, Web controls are an extension set of HTML controls. Web controls have more methods, attributes, and events to fully participate in the page lifecycle.
Connectivity of ASP. NET Server controls
All ASP. NET Server controls are inherited from the system. Web. UI. control class, which is also the basis of ASP. NET pages. The icomponent interface implemented by this class defines the interaction method between the control and other components running in the public language. The idisposeable implements the explicit release of the managed object memory.
The idatabindingaccessor interface of the control class defines a read-only set (databindings attribute), which contains a Fast Application Development (RAD) designer (such as 008 under) bind all data of the control to be used.
Server Control identifier: the client ID of the control is generated based on the uniqueid property value (this property value is the real server-side identifier generated by ASP. NET for each control ). The difference between the uniqueid and clientid attribute values is that the former uses the dollar sign ($), and the latter uses the underscore. The dollar sign appears only in the uniqueid string when the control named container is not a page.
ASP. NET generates the uniqueid attribute value based on the value specified by the programmer in the ID attribute. If no ID is specified, ASP. NET will automatically generate one for the control (for example, _ ctlx, where X is an incremental index starting from 0 ). If the control is named as the host page, the uniqueid directly uses the value of ID. Otherwise, uniqueid will get the id value with the name container ID prefix.
Naming container
A naming container is a control that serves as a container for other controls. In this way, the naming container will generate a virtual namespace.
Bind container
The bindingcontainer attribute of the bound container indicates which control in the page hierarchy represents the data bound to the parent control of the current control. In other words, the bound container can connect the bound data from the Host Control and send it down to the Child control.
Container binding and naming are often the same. The only example is when the control is part of the template. In this case, the namingcontainer attribute is generally set as the physical parent control of the control, and the bindingcontainer points to the control defined as a template.
Setting the visible attribute to false does not mean that no text is output. The control is still an active object with methods and can handle events. If a method directly sends the text to the output console through response. Write, the text is still displayed to the user.
Control Method
All child controls exist in the controls set (controlcollection type. This collection class has several features: it will perform post-processing on the controls added to the collection and the controls removed from it. After a control is added, the view status is restored and the view status tracking is enabled if necessary. After the control is removed, the unload event is triggered.
Control events
By using the rendercontrol method, all server controls are rendered as HTML. In this case, the prerender event is triggered.
Adaptive Rendering
Adaptive rendering means that the control can generate different tag codes for different browser types. This feature is implemented by delegating the generation of tags to the adapter. When each control is about to be rendered, it will specify its current adapter and pass the request to it.
The selection of the adapter depends on the current browser. By querying the browser features configured in the ASP. NET browser database, you can analyze the corresponding control adapters. If the browser record contains the adapter class for the given control, the class will be instantiated and put into use. Otherwise, the instance of the controladapter class, the default adapter of the control, is used.
By using the adapter property of the control, the control can save the reference pointing to the adapter instance. In addition to the composite controls rendered with child controls, each control has an associated adapter.
All ASP. NET controls have entry points of the rendering engine in the render method. The method signature is as follows:
Protected virtual void render (htmltextwriter writer ){...}
The render method calls an internal method, which is similar to the following pseudo code:
void RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
{
if(adapter != null)
{
adapter.BeginRender(writer);
adapter.Render(writer);
adapter.EndRender(writer);
}
else
{
this.Render(writer);
}
}
The adapter can be specified in declaration mode, and external components can be customized as needed. You only need to add a browser definition file to change the adapter.
The browser definition file has a. browser extension and contains the definition information for a specific browser. At runtime, Asp.net determines the current browser type and uses the configuration file to determine the browser function. The following code uses the menu control to demonstrate the registration of the control adapter (not for a browser ).
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.Menu" adapterType="Core35.MenuAdapter" />
</controlAdapters>
</browser>
</browsers>
Save the preceding code in the. Browser file and deploy it in the app_browsers folder of the ASP. NET application.
The definition of the adapter class should be as follows:
public class MenuAdapter : System.Web.UI.WebControls.Adapters.MenuAdapter
{
......
}
This class will override some methods, such as init, renderinintag, renderendtag, and rendercontents.
Browser-aware Rendering
In ASP. NET 2.0 and later versions, we can assign the value of the method for a specific browser to all the control attributes. Sample Code:
<asp:Button ID="Button1" runat="server" Text="I'm a Button" ie:Text="IE Button" mozilla:Text="Firefox Button" />
Control status
Some ASP. NET controls need to maintain a certain state between requests. For example, you can flip the current page of the control and the current sorting order of the Data Control. To this end, ASP. NET introduced the concept of "control State" (control state) from version 2.0 and separated it from the view State.
The control status is a set of key view status data required by the control. Because the control status plays an important role, its data is stored in another independent member variable, which is treated separately from the conventional view status. In this way, the control status will not be affected when the view status is disabled. Unlike the view status, you need to complete additional steps to use the control status.
First, each control that uses the control status needs to notify the page, and you need the control status. Since then, there are no restrictions on the data storage container class (such as viewstate), and data should be available from any object (such as arrays and collections ). By using a pair of override methods, each control can store and load its own control status, as shown below:
protected override object SaveControlState()
protected override void LoadControlState(object state)
The control state works in a similar way as the view State. It is loaded at the same stage as the view State in the pipeline. In the end, the two are stored in the same hidden field.
Input focus
The page class starting with ASP. NET 2.0 provides a setfocus method to set the input focus on any control.
Sample Code:
void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
SetFocus("txtLastName");
}
The setfocus method caches the control's ID and enables the page class to generate necessary script code during rendering.