Alternative controls in LiteralControl ASP.NET

  • 2020-05-16 06:47:46
  • OfStack

First look at part of an aspx file:
 
<!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 parses this document into a hierarchical, recursive control tree similar to Dom. The top layer is the page Page, so now we have to ask, how many controls are in the next layer of Page?
The answer is 5, can output this Page. Controls. Count value validation 1. How does it parse?
Number 1: from < ! DOCTYPE to transitional. dtd" > \r\n
Number 2: from < head id = "Head1" < /head > , of type HtmlHead;
Number 3: include < /head > The back \ r \ n \ < body > \r\n\t
Number four: from < form id = < /form > , of type HtmlForm;
Number 5: from < /form > \r\n at the end.
The second and fourth belong to the Html control, while the first 1.3.5 belong to the static text, which is parsed by ASP.NET into LiteralControl. LiteralControl is neither an Web control nor an Html control, but a class of its own, representing HTML elements, text, and any other strings in the ASP.NET page that do not need to be processed on the server. You could almost say that LiteralControl is a control that is definitely used in ASP.NET, but you might not know it.
There are several notes about LiteralControl:
1. LiteralControl can also have ID, ClientID, and UniqueID, but 1 is rarely used. It makes little sense to set ID, other than to use the FindControl method.
2. LitrelControl does not have view state, and although it does have an EnableViewState property, it does not work, and the previously changed state is lost after the postback.
3. Because it represents static text, you cannot set any style for LitrelControl itself.
4. The background gets and sets the value through its Text property.
. net LiteralControl explanation
For the application of LiteralControl control is less, today suddenly saw, it is better to make a clear, so summed up, for everyone to learn 1.
First, do the test yourself
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<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?
For example, if you have a server control on a page, ASP.net will create two LiteralControl objects that represent the static content before and after the control. Two server controls, and accordingly three LiteralControl objects. Let's do another example
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<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 form Where it ends  
</body> 
122233 
</html> 

Display the outer control type and its ID (LiteralControl displays the HTML text) 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 where form ends 122233 5556665
This example clearly shows that the text control between two server controls is such content.
Second, application
LiteralControl class
Represents HTML elements, text, and any other strings in the ASP.NET page that do not need to be processed on the server.
ASP.NET compiles all HTML elements and readable text that do not require server-side processing into instances of this class. For example, an HTML element that does not contain the runat="server" attribute/value pair in the start tag will be compiled as an LiteralControl object. The LiteralControl object does not maintain view state, so the contents of the LiteralControl object must be recreated for each request.
The text control behaves like text holder 1, which means you can extract text from the text control and remove the text control from the ControlCollection collection of the parent server control through the Controls property of the parent server control. Therefore, when developing custom controls derived from the LiteralControl class, make sure that the controls themselves perform any required pre-processing steps, rather than using calls to the LiteralControl.Render method to do so. Typically, this is done to improve the response time of the Web application.
You can programmatically add or remove text controls from a page or server control using the ControlCollection.Add or ControlCollection.Remove methods, respectively.
Code:
 
HtmlTableCell c = new HtmlTableCell(); 
c.Controls.Add( new LiteralControl(" line  " + j.ToString() + ",  column  " + i.ToString()) ); 
r.Cells.Add(c); 

The purpose of this is to improve the response time of the web application. c.InnerHTML ="" is the same as c.InnerHTML =".

Related articles: