Runyl blog park I want to comment (1) font size: T | T
The control. Render method sends the server control content to the provided htmltextwriter object, which is written in the content that will be presented on the client. This article mainly introduces the render method in ASP. NET. Let's take a look.
AD: the 2013 global big data Technology Summit is selling tickets at a low price
Asp.netAll controls are from the system. Web. UI. control class. Three rendering-related methods are defined in the control class, which areRenderMethod, renderchildren method and rendercontrol method. The rendercontrol method is public. First, let's look at the implementation of these three methods:
- Public void rendercontrol (htmltextwriter writer)
- {
- // Determine whether the visible attribute is true. If yes, call the render method to render the control. Otherwise, the control is not displayed.
- If (visible)
- {
- Render (writer );
- }
- }
- Protected virtual void render (htmltextwriter writer)
- {
- // Write the code for rendering the control itself here
- .......
- // Call the renderchildren method to present the child control of the control
- Renderchildren (writer );
- }
- Protected virtual void renderchildren (htmltextwriter writer)
- {
- // Call the rendercontrol method in each sub-control to present the sub-control cyclically, and recursively render the control tree of the entire page
- Foreach (control C in controls)
- {
- C. rendercontrol (writer );
- }
- }
The rendercontrol method is used by external classes to generate controls. For example, a parent Control calls the rendercontrol method of a child control. the rendercontrol method only determines whether to display the control. If it is displayed, it calls the control's protected method render.
The render method is the core method for rendering controls. In a real-time custom control, we generally rewrite the render method to render the control. If the control is a container control, override the renderchildren method to present the child control.
All the server-side controls are from the system. web. UI. derived from webcontrol. webcontrol is derived from control. Therefore, webcontrol has the preceding three methods, but several methods are added. It divides the render method into three methods: renderbgegintag, rendercontents, renderendtag. the implementation code of render is as follows:
- Protected override void render (htmltextwriter writer)
- {
- // Present the start tag
- Renderbgegintag (writer );
- // Display the TAG content
- Rendercontents (writer );
- // Display the end tag
- Renderendtag (writer );
- }
- Public Virtual void renderbegintag (htmltextwriter writer)
- {
- // Call the addattributestorender method to add tag attributes
- Addattributestorender (writer );
- // Determine whether the displayed tag is a known tag;
- Htmltextwritertag tagkey = tagkey;
- If (tagkey! = Htmltextwritertag. Unknown)
- {
- Writer. renderbegintag (tagkey );
- }
- Else
- {
- // Unknown tag, the specified tagname attribute is used
- Writer. renderbegintag (this. tagname );
- }
- }
- Protected virtual void rendercontents (htmltextwriter writer)
- {
- // If You Want To render the child control, you must call the render method of the base class.
- Base. Render (writer );
- }
Looking at these methods, I don't think there is much need to break down the render method into three methods. This makes sense only for a control that presents a single label, composite Controls seem meaningless and increase complexity.
Summary:
1. if derived from control, the render () method should be reloaded to render the control. if the control is a container control, the base of the base class should be called in the render method. the renderchildren () method to present the child control.
2. if you derive from the webcontrol class, there are two situations: one is to use the tagkey attribute to generate the output HTML Tag. In this case, you should overload the rendercontents () method to present the control. the second is not to render the default HTML Tag generated with tagkey. In this case, the render () method should be reloaded to present the control, as shown in the following code:
- Protected virtual void render (htmltextwriter writer)
- {
- // Write the code for rendering the control itself here
- Addattributestorender (writer );
- Rendercontents (writer );
- }
- Protected virtual void rendercontents (htmltextwriter writer)
- {
- // Present the control code
- ....
- // If You Want To render the child control, you must call the render method of the base class.
- Base. Render (writer );
- }
3. If the control is derived from the webcontrol class and is a container control, you should call the base. Render () method in rendercontents to present the child control.
The other two methods will be introduced in subsequent articles. Hope to help you.
[Edit recommendations]
- Asp.net authentication method forms
- Design of large-scale high-performance ASP. NET System Architecture
- Use applications in iis7 using ASP. NET
- View changes in ASP. net mvc 3
- 10 most useful attributes of ASP. NET controls