LiteralControl ASP. NET's alternative controls _ practical tips

Source: Internet
Author: User
First look at some of the content in an ASPX file:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
</form>
</body>

We know that ASP.net will parse this document into a DOM-like control tree, which is hierarchical and recursive. One of the top pages is page, so now ask, how many controls are there on the next level of page?
The answer is 5 and you can output this. Verify the value of the Page.Controls.Count. How does it come to be resolved?
The first one: from <! DOCTYPE to TRANSITIONAL.DTD ">\r\n
The second one: from Third: including the \r\n\<body>\r\n\t behind the Fourth: from <form id= to </form&gt, type HtmlForm;
Fifth: From the </form> behind the \ r \ n to the last.
The second and fourth belong to HTML controls, while 第一、三、五个 is static text, ASP. NET to parse it into LiteralControl. LiteralControl is neither a Web control nor an HTML control, and is a class that represents any other string that is not required to be processed on the server in HTML elements, text, and ASP.net pages. It's almost certain that LiteralControl is a control that is definitely used in asp.net, but you may not know it.
on the LiteralControl, there are several notes:
1. LiteralControl can also have IDs, ClientID, and UniqueID, but they are rarely used. In addition to using the FindControl method, it does not make sense to set the ID.
2. Litrelcontrol does not have view state, although it also has a EnableViewState property, but it does not work, the state of the previous change after postback will be lost.
3. Because static text is represented, no style can be set for the Litrelcontrol itself.
4. The background gets and sets the value through its Text property.
. NET LiteralControl detailed
The application of LiteralControl control is relatively few, today suddenly see, get a understand for good, so summed up, for everyone to study together.
First, do your own tests.
Copy Code code as follows:

<title > Untitled page </title>
<body id= "Body1" >
<form id= "Form1" runat= "Server" >
<div id= "Div1" >
<div id= "Div2" >
</div>
</asp:Panel>
</div>
</form>
</body>

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 five controls, two of which are server control head and form. But what about the other three?
It's 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, corresponding to three LiteralControl objects. Let's look at an example.
Copy Code code as follows:

<title > Untitled page </title>
<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 where the form ends.
</body>
122233

The type of control that displays the outer layer and its ID (the HTML text that LiteralControl displays) is 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 place where form ends 122233 5556665
This example is clearly illustrated by the text control between two server controls.
Second, the application
LiteralControl class
Represents any other string that is not required 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 require server-side processing to an instance of the 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 view state, so the contents of the LiteralControl object must be re-created for each request.
The text control behaves like a text host, which means that text can be extracted from a text control and the text control is removed from the parent server control's ControlCollection collection by the Controls property of the parent server control. Therefore, when you develop a custom control that derives from the LiteralControl class, make sure that the control performs any required preprocessing steps on its own, rather than using a call to the Literalcontrol.render method to do the operation. Typically, this is done to increase the response time of the WEB application.
You can programmatically add or remove text controls from a page or server control by using the Controlcollection.add or Controlcollection.remove method, respectively.
Code:
Copy Code code as follows:

HtmlTableCell C = new HtmlTableCell ();
C.controls.add (New LiteralControl ("line" + j.tostring () + ", column" + i.tostring ()));
R.cells.add (c);

This is done to improve the response time of the Web application, which is essentially written in C. Innerhtml= "" is the same, but is to apply the object-oriented thinking.
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.