First, let's look at some content in An aspx file:
Copy codeThe Code is as follows: <! 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 id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
</Form>
</Body>
</Html>
We know that ASP. NET will parse this document into a control tree similar to Dom. The control tree is layered and recursive. The top-level Page is the Page. How many controls does the next Page have?
The answer is five. You can verify the value of this. Page. Controls. Count. How is it resolved?
First: from <! DOCTYPE to transitional. dtd "> \ r \ n
Second: from Third: Include \ r \ n \ <body> \ r \ n \ t after Fourth: from <form id = to </form>, the type is HtmlForm;
Fifth: From \ r \ n after </form> to the end.
The second and fourth are Html controls, while the first, third, and fifth are static texts. ASP. NET parses them into LiteralControl. LiteralControl is neither a Web control nor an Html control. LiteralControl is a type of control that represents any other strings that do not need to be processed on the server in HTML elements, text, and ASP. NET pages. It can be said that LiteralControl is definitely used in ASP. NET, but you may not know it.
LiteralControl:
1. LiteralControl can also have ID, ClientID, and UniqueID, but it is rarely used. Besides using the FindControl method, setting IDs does not make sense.
2. LitrelControl does not have a view State. Although it also has an EnableViewState attribute, it does not work. The status changed before sending back will be lost.
3. Because it represents static text, you cannot set any style for LitrelControl itself.
4. The background obtains and sets the value through its Text attribute.
. Net LiteralControl
LiteralControl has few applications. Today, I suddenly saw it, so I figured it out, so I will summarize it for everyone to learn together.
First, perform the test on your own.Copy codeThe Code is as follows: <Head runat = "server" id = "head1">
<Title> No title page </title>
</Head>
<Body id = "body1">
<Form id = "form1" runat = "server">
<Div id = "div1">
<Div id = "div2">
</Div>
</Asp: Panel>
</Div>
</Form>
</Body>
</Html>
When the above Code reads all types of the outermost control:
System. Web. UI. LiteralControl-
System. Web. UI. HtmlControls. HtmlHead-head1
System. Web. UI. LiteralControl-
System. Web. UI. HtmlControls. HtmlForm-form1
System. Web. UI. LiteralControl-
You can see that there are five controls, two of which are server controls head and form. But what are the other three?
In fact, it is very simple: for example, if there is a server control on the page, ASP.net will create two LiteralControl objects, representing the static content before and after the control, respectively. Two server controls have three LiteralControl objects. Let's look at another example.Copy codeThe Code is as follows: <Head runat = "server" id = "head1">
<Title> No title page </title>
</Head>
<Body id = "body1">
Bogy343242
<Form id = "form1" runat = "server">
<Div id = "div1">
<Asp: Panel ID = "Panel1" runat = "server" Height = "50px" Width = "125px">
<Div id = "div2">
</Div>
</Asp: Panel>
</Div>
</Form>
This should be the end of form.
</Body>
122233
</Html>
The type and ID of the control whose outer layer is displayed (the HTML text in the control is displayed in LiteralControl) are as follows:
System. Web. UI. LiteralControl-
* ** Text: 1231232321 html23232
System. Web. UI. HtmlControls. HtmlHead-head1
System. Web. UI. LiteralControl-
* ** Text:/headerefefe bogy343242
System. Web. UI. HtmlControls. HtmlForm-form1
System. Web. UI. LiteralControl-
* ** Text: this should be the end of form 122233 5556665
This example clearly shows that the text control between two server controls is like this.
Second, Application
LiteralControl class
Any other string that does not need to be processed on the server in HTML elements, text, and ASP. NET pages.
ASP. NET compiles all HTML elements and readable text that do not need to be processed by the server into an instance of this class. For example, HTML elements that do not contain runat = "server" attribute/value pairs in the start tag will be compiled into LiteralControl objects. The LiteralControl object does not maintain the view State. Therefore, you must recreate the content of the LiteralControl object for each request.
The behavior of the text control is the same as that of the text control, which means that the text control can be extracted from the text control and removed from the ControlCollection set of the parent server control through the Controls attribute of the parent Server Control. Therefore, when developing a custom control derived from the LiteralControl class, make sure that the control executes any required preprocessing steps, instead of calling the LiteralControl. Render method to complete these operations. This is usually done to increase the response time of Web applications.
You can use the ControlCollection. Add or ControlCollection. Remove methods to Add or Remove text controls from pages or server controls programmatically.
Code:Copy codeThe Code is as follows: HtmlTableCell c = new HtmlTableCell ();
C. Controls. Add (new LiteralControl ("row" + j. ToString () + ", column" + I. ToString ()));
R. Cells. Add (c );
The purpose of this operation is to improve the response time of web applications. Its essence is the same as that of c. InnerHTML = "", but it only applies the object-oriented approach.