Introduction to RenderContents of ASP. NET Server controls:
This article focuses on another common method to implement control rendering-use the RenderContents method of the WebControl class to implement control rendering.
Basic knowledge
There are only two cases for ASP. NET Server controls: one is a control with visual elements, and the other is a control without visual elements. If the server control to be developed contains visual elements, it is recommended that developers create a control class inherited from the System. Web. UI. WebControls. WebControl base class in most cases. This approach is mainly based on convenience considerations. The WebControl class provides public attributes, methods, and events related to the appearance of some server controls. This class defines attributes to control the appearance and behavior of server controls. For example, the BackColor and ForeColor attributes can respectively control the background color and foreground color of the server control. On the controls that can display borders, you can set the BorderWidth, BorderStyle, and BorderColor attributes, controls the Border Width, border style, and border color. The size of the server control can be specified through the Height and Width attributes. If the Control base class is a Control class, implementing such similar content is cumbersome.
When using the WebControl base class to render ASP. NET Server controls, you must use the attributes, methods, and other member objects provided by this class. This is what readers need to master. In addition, the constructors of the base class cannot be ignored. The following describes the WebControl constructor and describes common member objects.
The WebControl class contains three constructor functions, which are used to initialize new instances of the WebControl class. However, there are some minor differences between them.
◆ Protected WebControl ()
This constructor is used to initialize a new instance of the WebControl class that represents the Span HTML element. Generally, developers do not directly call this constructor. Instead, it is usually called by the constructor of the derived class to initialize the TagKey attribute as the Span enumerated value. In the subsequent example, the TagKey attribute will be rewritten to call this constructor.
◆ Public WebControl (HtmlTextWriterTag tag)
Developers can use this constructor to create and initialize a new instance of the WebControl class that uses the specified System. Web. UI. HtmlTextWriterTag value. The parameter tag indicates one of the enumerated values of HtmlTextWriterTag. Readers may not be familiar with HtmlTextWriterTag. It is an enumeration type, and its enumeration values are mostly HTML tags, such as A, B, Bold, and Button.
◆ Protected WebControl (string tag)
You can use this constructor to create and initialize a new instance of the WebControl class marked with the specified HTML. The parameter tag indicates the HTML tag. When using this constructor, you must note that you cannot directly call this constructor. Instead, it is usually called by the constructor of the derived class to initialize the TagKey and TagName attributes.
After learning about the WebControl constructor, you must also understand some common attributes and methods of the WebControl class. These common member objects are listed below, which are of great significance for implementing ASP. NET Server Control rendering.
1) Attributes
This attribute is used to obtain a set of attributes that do not correspond to the properties of the control and is only used for rendering). Its Attribute type is AttributeCollection.
2) ControlStyle attributes
This attribute is used to obtain the Style of the server control, which is of the Style type. The ControlStyle attribute encapsulates all appearance attributes of the WebControl class, such as BorderColor and Font.
3) TagKey attributes
This attribute is used to obtain the System. Web. UI. HtmlTextWriterTag value corresponding to this server control. Its Attribute type is HtmlTextWriterTag enumeration.
4) protected virtual void AddAttributeToRender (HtmlTextWriter writer );
This method adds HTML attributes and styles to the specified System. Web. UI. HtmlTextWriter. Note that the corresponding methods in the base class must be called during the rewriting process.
5) public void ApplyStyle (Style s );
This method copies all non-blank elements of the specified style to the control and overrides all existing style elements of the control.
6) public void MergeStyle (Style s );
This method copies all non-blank elements of the specified style to the control, but does not overwrite any existing style elements of the control.
7) protected override void Render (HtmlTextWriter writer); Method
This method overwrites Control. Render.
8) protected virtual void RenderContents (HtmlTextWriter writer );
This method presents the content of the ASP. NET Server Control to the specified writer. If you want to write text or other content to the control label, you need to override this method. If you want to use the default logic to present the child control, you must call the corresponding methods in the base class.
You may have noticed two methods in the WebControl base class: Render and RenderContents. According to the content described above, the Control base class includes the Render method. Because the WebControl class inherits from the Control class, it is beyond reproach to include the Render method in the WebControl class. However, there is an RenderContents method in the WebControl class, and this method is very similar to the Render method in terms of functions and parameters. Which one should be used in rendering controls?
In fact, normally, if the server control is derived from the WebControl base class, the Render method is rarely used, and the RenderContents method is mainly used to Render the control. To illustrate the causes, we must understand the implementation logic of the Render method in the WebControl base class.
The implementation schematic code of the Render method in the WebControl base class is as follows:
- protected override void Render(HtmlTextWriter output)
- {
- RenderBeginTag(output);
- RenderContents(output);
- RenderEndTag(output);
- }
The implementation of the RenderBeginTag method in the WebControl base class is as follows:
- public virtual void RenderBeginTag(HtmlTextWriter output)
- {
- AddAttributesToRender(output);
- HtmlTextWriterTag tagKey = TagKey;
- if(tagKey != HtmlTextWriterTag.Unknown)
- {
- output.RenderBeginTag(tagKey);
- } else {
- output.RenderBeginTag(this.TagName);
- }
- }
The implementation of the RenderContents method in the WebControl base class is as follows:
- Protected Override VoidRenderContents (HtmlTextWriter output ){
- // Use the default logic to present the child control. Therefore, you must call the methods in the base class.
- Base. Render (output );
- }
By analyzing the code above, we can draw the following conclusions:
1. To implement control rendering in a class derived from WebControl, you must overwrite one or more methods such as AddAttributesToRender, RenderBeginTag, RenderEndTag, and RenderContents without rewriting the Render method.
2. Rewrite AddAttributesToRender, RenderBeginTag, RenderEndTag, RenderContents, and other methods. Note the conditions and precautions for rewriting these methods. Otherwise, the server control may lose tags, this will seriously affect the rendering of server controls.
3. When the content in the ASP. NET Server Control label is displayed, the RenderContents method must be rewritten.
This section describes some basic knowledge about the WebControl class. Especially for the schematic Code listed above, you need to understand it. This plays an important role in implementing control rendering.
The basic content of RenderContents of ASP. NET Server controls is introduced here. I hope to help you understand RenderContents of ASP. NET Server controls.
- Analysis on the use of the Render method for ASP. NET Server controls
- Development of ASP. NET Server controls
- Analysis of ASP. NET Server Control Processing and return data
- Analysis on capture and return events of ASP. NET Server controls
- Analysis of ASP. NET control development-based event processing