Render Method for ASP. net2.0 server controls (Author: metal edge Source: Tianji Development)

Source: Internet
Author: User
Address: http://dev.yesky.com/msdn/228/2411728.shtml

Control rendering is the process of writing markup text to the HTTP output stream. The server sends the generated markup text to the client through the HTTP output. The text will be converted to visualized elements through the client browser. Display with controls, developers can tag HTML and scriptCodeAnd CSS style sheets. There are two main ways to render server controls: one is the control render method and the other is the webcontrol rendercontents method. This article describes how to use the control render method to display controls.

Use the htmltextwriter class

The render method of the control class is mainly used to implement control rendering. The declared code is as follows:

Protected virtual void render (htmltextwriter output)

As shown in the above Code, the parameter of the render method is of the htmltextwriter type. To better apply the render method, you should first understand the htmltextwriter class and its related content.

According to the description in msdn2005, The htmltextwriter class is used to write the markup characters and text into the output stream of the ASP. NET Server Control. This class provides the format setting function used by ASP. NET Server controls to present tags to clients. To implement the class function, the htmltextwriter class defines multiple fields, attributes, and methods. Due to the large number of member objects, this article only selects some common members to describe, and also introduces some newly added members of ASP. NET 2.0.

Common Member objects include:

· Addattribute Method

For the htmltextwriter object, add the specified tag attribute and value to the start tag of the element created by subsequent calls to the renderbegbegintag method.

· Addstyleattribute Method

For the htmltextwriter object, add the tag style attribute to the start tag of the element created by subsequent calls to the renderbegintag method.

· Write Method

Write the specified data type together with any pending tab spacing to the output stream.

· Writeattribute Method

Write tag attributes and their values to the output stream.

· Writebegintag Method

Any tab spacing and the start flag of the specified tag element are written to the output stream.

· Writeendtag Method

Write any tab spacing and end tag of the specified tag element.

· Encoding attributes

Obtains the encoding of the htmltextwriter object used to write content into the page.

· Indent attributes

Gets or sets the number of tabs used to indent the start position of each line.

· Newline attributes

Gets or sets the line terminator string used by the htmltextwriter object.

For beginners, it is recommended that you master the application of the above Member objects. In addition, ASP. NET 2.0 adds some members to the htmltextwriter class, including:

· Beginrender Method

Notifies the htmltextwriter object or the object of the derived class. A control is displayed.

· Endrender Method

Notifies the htmltextwriter object or the object of a derived class that a control has been rendered.

· Isvalidformattribute Method

Check an attribute to make sure it can be rendered in the start tag of the <form> tag element.

· Writeencodedurl Method

Encode the specified URL and write it to the output stream. The URL can contain parameters.

· Writeencodedtext Method

Encode the specified text of the requested device and write it to the output stream.

· Writebreak Method

Write the <br/> flag element to the output stream.

Render controls using the render Method

1. Basic Knowledge

The render method described in this article belongs to the system. Web. UI. Controls. control class. This class is the base class for creating server controls. Many control classes inherit from this class. The control class contains three methods for rendering controls: render, renderchildren, and rendercontrol. Both use htmltextwriter instances as parameters. They allow the server control content to be provided for an htmltextwriter object and encapsulate its content into an HTTP output stream and output it to the client for display. The following is a brief introduction to these three methods.

(1) protected virtual void render (htmltextwriter writer );

This method is used to send the server control content to the provided htmltextwriter object, which is written into the content that will be presented on the client. When developing a server control, you can override this method to render the server control.

(2) protected virtual void renderchildren (htmltextwriter writer );

This method is used to output the child-level content of the server control to the provided htmltextwriter object, which is written in the client. This method notifies ASP. Net of All Active Server Pages On the rendering page. If there is no ASP code on the page, this method will render all the child controls of the server control.

(3) protected virtual void rendercontrol (htmltextwriter writer); and protected void rendercontrol (htmltextwriter writer, controladapter adapter)

Rendercontrol has two overload methods, which are used to output the content of the server control to the provided htmltextwriter object. If the tracking function is enabled, trace information about controls is stored. If the visible attribute of the server control is set to true, this method determines whether the page tracking function is enabled. If enabled, it stores control-related trace information and presents the content of the server control to the page. In addition, the previous overload method is inherited by ASP. NET 2.0 from ASP. NET 1.0, and the last overload method is added by ASP. NET 2.0. The latter uses the provided controladapter object to output the server control content to the provided htmltextwriter object. The adapter parameter is of the controladapter type and is used to define the rendered controladapter. This method is commonly used to implement server controls running on various devices and browsers.

The above three methods seem to be independent of the three methods. However, there is actually a close relationship between them. Readers can understand the relationship between them by reading the following schematic code.

// Basic implementation of the rendercotrol Method
Public void rendercontrol (htmltextwriter output)
{
If (visible)
{
Render (output );
}
}
// Basic implementation of the render Method
Protected virtual void render (htmltextwriter output)
{
Renderchildren (output );
}
// Basic implementation of the renderchildren Method
Protected virtual void renderchildren (htmltextwriter output)
{
Foreach (control C in controls)
{
C. rendercontrol (output );
}
}

The code above lists the Implementation ideas of the rendercontrol, render, and renderchildren methods. Obviously, these three methods play a role in the control rendering process, and a simple recursive call process is used. In general, it can be understood:

(1) Create an htmltextwriter class instance in the page framework;

(2) The page framework passes the instance object to the rendercontrol method;

(3) The rendercontrol method checks whether the visible property visible of the control is true. If the value is true, the rendercontrol method calls the render method. If the value is false, the Control and Its subcontrols are not displayed;

(4) The render method is implemented by default and the renderchildren method is called;

(5) The renderchildren method calls the rendercontrol method of each sub-control according to the settings in the default implementation;

In fact, it does not matter if you cannot understand the above process in a short time. For beginners, the key is to remember the most important and commonly used render method. The control developer can override the render method to complete the rendering control task.

2. Example Application

The previous section describes the basic knowledge of using the control render method to render controls. The following is a typical example to help you understand how to use render. Example 1 is shown.


Figure 1

As shown in 1, the server control presents a hyperlink and sets the text to red. When a user clicks the red text, the page is directed to the Microsoft Site. Of course, you can set the hyperlink address through the link URL attribute.

The following is an example.Source code.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace usingrendercontrol
{
[Defaultproperty ("linkurl")]
[Toolboxdata ("<{0}: rendercontrol runat = Server> </{0}: rendercontrol>")]
Public class rendercontrol: Control
{
// Implement linkurl
[Bindable (true)]
[Category ("appearance")]
[Defaultvalue ("http: // localhost/")]
[Localizable (true)]
Public String linkurl
{
Get {string S = (string) viewstate ["linkurl"];
Return (S = NULL )? String. Empty: S );}
Set {viewstate ["linkurl"] = value ;}
}
// Rewrite the render Method
Protected override void render (htmltextwriter writer)
{
Writer. addattribute (htmltextwriterattribute. href, linkurl );
Writer. addstyleattribute (htmltextwriterstyle. color, "Red ");
Writer. renderbegintag (htmltextwritertag. );
Writer. Write ("Browse website ");
Writer. renderendtag ();
}
}
}

The code above implements the rendercontrol class for custom server controls. This class inherits from the control base class, implements the linkurl (default value: http: // localhost/) that represents the hyperlink address, and overwrites the render method. During render rewriting, some htmltextwriter class members are called, such as writer, addattriattribute, addstyleattribute, renderbegintag, and renderendtag. In addition, developers may need to present a long string when using the writer method. We recommend that you use the writer method instead of the string cascade or stringbuilder method. Because it will consume a lot of system time and memory, and the efficiency is low.

Some readers may ask, if the order of the multi-line code displayed by the server control changes, what is the difference? For example, if you first apply the renderbegintag, write, and renderendtag methods in sequence, and then call the addattribute and addstyleattribute methods, will the same effect be displayed? The answer is no. It should be emphasized that, in the process of rendering the control, you must first define the attributes and CSS styles of the server control, and then define the main content of the server control, this order cannot be changed.

In addition, if you are interested, you can implement a text attribute in the code to obtain or set the text displayed by the control. In this way, the "Browse Website" text output by using the write method can be replaced by the text attribute in the render method.

The source code of the default. aspx file created to use the above custom Server Control is listed below.

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<% @ Register tagprefix = "sample" assembly = "usingrendercontrol" namespace = "usingrendercontrol" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> display controls using the render method </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Sample: rendercontrol runat = "server" id = "customercontrol" linkurl = "http://www.microsoft.com/">
</Sample: rendercontrol>
</Div>
</Form>
</Body>
</Html>

The above code is relatively simple, which mainly declares the custom Server Control rendercontrol, and set its linkurl property value to http://www.microsoft.com, that is, the Microsoft site address.

When you run the preceding page in a browser and view the relevant HTML source files, you can get the following code:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> display controls using the render method </title>
</Head>
<Body>
<Form name = "form1" method = "Post" Action = "default. aspx" id = "form1">
<Div>
<Input type = "hidden" name = "_ viewstate" id = "_ viewstate" value = "/wepdwujnzmymty5ntu2zgqqyrld/g + vm1h41r2cekxid63o5g ="/>
</Div>
<Div>
<A href = "http://www.microsoft.com/" style = "color: red;"> browse websites </a>
</Div>
</Form>
</Body>
</Html>

By observing the above code, we can see that the result of the custom Server Control rendercontrol is the code shown in bold, which is finally displayed as a <A> flag indicating a hyperlink.

Summary

This article first introduces the basic knowledge of the htmltextwriter class, and then explains the application of using the render method to implement control rendering. In the subsequent articleArticle. In general, the control rendering technology is the most common and simple content in the development process. It is recommended that you be familiar with the content.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.