The display of the Asp.net control is naturally inseparable from the display of HTML, CSS, JavaScript, and other foreground content. Therefore, the first thing to do when developing a control is to knowHow to output the content to be displayed on the client
Because the control eventually exists in the ASP. NET Framework, it must have a base class that can be integrated in the ASP. NET Framework.
1. Select base class
First, let's talk about ASP. all the standard controls in. Net can be used as the base class. Therefore, if your controls only make a few modifications or additions to a standard control, you can use this standard control as a base class. Generally, if the developed control cannot be found from the standard, it can be inherited from three classes:
System. Web. UI. Control
System. Web. UI. webcontrols. webcontrol
System. Web. UI. webcontrols. compositecontrol The following describes the relationships and differences between the three classes:
Control: onlySimple presentation, Does not support CSS. For example, literal control
Webcontrol: controls are createdAppearance support. Suitable for visual control inheritance, such as: button
Compositecontrol: Multiple controls are derived.Compound. It is suitable for developing standard controls in application Asp.net.
Relationship: control is the base class of all controls of Asp.net,Webcontrol is inherited from control,Compositecontrol is inherited from webcontrol (a class added in ASP. net2.0 ).Although the three basic classes have their own strengths, we cannot forget the inheritance relationship between them. So we will analyze the display behavior of controls from the control class.Ii. How to present
1. display of control
The rendering in the control class is implemented through the method render. Render prototype:
Protected Internal Virtual VoidRender (htmltextwriter writer ){}
The htmltextwriter writer parameter is provided by the framework that calls the render method at runtime, so we can rewrite the render method to render the content.
Public Class Helloworld: Control {
Protected Override Void Render (htmltextwriter writer)
{
Writer. writeline ("Hello World" );
}
} In the render method, we can implement the output HTML Tag and style by using the following enumeration: htmltextwritertag, htmltextwriterattribute, and htmltextwriterstyle. Htmltextwritertag indicates HTML tags, htmltextwriterattribute indicates attributes on tags, and htmltextwriterstyle indicates styles. AboveCodeYou only need to write the rendering logic. when to call the rendering, how to call it, and how to prepare the htmltextwriter instance is a concern during ASP. NET running.
2. How is render () called? Page is indirectly inherited from the control class. The default logic of the control rendercontrol () method is to judge the control. visible attribute. If it is true, the control. Render () method and the render method are called. The default logic is to generate your own content and call the rendercontrol () method to trigger the generation of all child controls (if any, the child control will also call rendercontrol () and render () in the same order, and call the renderchildren () method to trigger the generation of the Child control, so that the first-level recursion continues, the Control tree is generated. 3,
Webcontrol Rendering
The presentation of webcontrol consists of three steps: rendering the start tag, rendering the content in the tag, and rendering the end tag. The methods are: renderinintag, rendercontents, and renderendtag. The tag generated by renderbegintag is determined by the webcontrol. tagkey or webcontrol. tagname attribute. The default tag of webcontrol. tagkey is <span>. To change the current tag, you can rewrite webcontrol. tagkey or webcontrol. tagname.
Note that when we want to control the peripheral labels, we will not rewrite the renderbegintag method, but will rewrite the tagkey attribute. In addition, if we rewrite the renderbegintag method, we must rewrite the renderendtag method.
In addition, webcontrol provides the addattributetorender method to add control attributes. Note that when you override the addattributetorender method to add attributes, you must also call the base. addattributetorender method. The webcontrol class converts control. Render () to zero. 4,
compositecontrol
because compositecontrol inherits from webcontrol, the attribute tagkey is also used to determine the start tag. To implement compositecontrol rendering, you only need to add a domain. For example, if our control requires a Textbox Control, it can be expressed as private textbox _ txtinput. Then, rewrite the createchildcontrols method and use this. Controls. Add method to render it.