All controls in ASP.NET are from the System.Web.ui.Control class, define three ways related to presentation in the Control class, named the render method, renderchildren method, and render method. The renderControl method is public Method, first look at the implementation of these three methods:
Public void renderControl (HTMLTextWriter Writer)
{
// Determine if the Visible property is true, if so, call the render method to present the control, otherwise the control is not presented.
IF (visible)
{
RENDER (Writer);
}
}
Protected Virtual Void Render (HTMLTextWriter Writer) {// Write the code of the control itself here ....... // Call the renderchildren method to present the child control renderchildren (Writer);}
Protected Virtual Void RenderChildren (HTMLTextWriter Writer) {// Cycle Call Each sub-control RenderControl method Rendering child control, recursive implementation of the control tree for the entire page Foreach (Control C in Controls) {C.RenderControl (Writer);}}
The renderControl method is used to call to generate controls, such as a render, such as a parent control calling sub-control. In the renderControl method, only the determination of the control is displayed, if the display is displayed, the control method of the control render.render method It is the core method of presenting controls. In realistic custom controls, we generally render controls by overwriting the render method. If the control is a container control, you will render the renderchildren method to render the child control.
All server controls are derived from System.Web.ui.WebControl. WebControl is derived from Control, so WebControl has three methods described above, but adds several ways, it separates the render method into three methods: RenderBgeginTag, RenderContents, RenderEndTag.Render the codes are as follows: Protected override void render (HtmlTextWriter writer) {// presentation start tag RenderBgeginTag (writer); // render the content RenderContents (writer) tags; // render End the label renderendtag (Writer);
Public virtual void RenderBeginTag (HtmlTextWriter writer) {// call AddAttributesToRender method, add attributes AddAttributesToRender (Writer) tags; // render determining whether the label is a label known; HtmlTextWriterTag tagkey = TagKey;! If (tagkey = HtmlTextWriterTag.Unknown ) {Writer.Renderbegintag (tagkey);} else {// unknown tag, use the specified tag name TAGNAME attribute Writer.Renderbegintag (THISTAGNAME);}}
Protected Virtual Void RenderContents (HTMLTEXTWRITER WRITER) {// If you want to render a child control, you will call the render method base.render (Writer) of the base class;
Look at these methods, I think there is not much necessary to break the render method into three methods, so that only the control of a single label is meaningful. For the composite control, there is no significance, but also adds complexity. Summary: 1. If you derive it from Control, you should overload the render () method to render the control. If the control is a container control, you should call the base.RenderChildren () method of the base class in the render method, rendering child control. 2 If you derive it from the WebControl class, one is to generate an output HTML tag using the TagKey property. At this time, it should be rendered to render the control. The second is to generate the default use of TagKey HTML tag, then rendering the render () method to render the control, as shown below: protected virtual void render (HTMLTextWriter Writer) {// Write the code ADDATTRIBUTESTORENDER (Writer) of the control itself here; renderContents (Writer) } Protected Virtual Void RenderContents (HTMLTextWriter Writer) {// Rendering this Control code ....
// If you want to render a child control, you will call the base method base.render (Writer);} 3. If you are born from the WebControl class, and the control is a container control, you should call Base.Render in renderContents. () Method to present a child control.