Antique---ASP. aspx page runat= "Server"

Source: Internet
Author: User

Since MVC3 has been extensively promoted production environment, this runat= "server" is slowly forgotten by people ...

Does ASP. WebForm control-based HTML rendering process remember? Do you remember that lump of control?

HtmlTextWriter, do you remember?

The life cycle of the fascinating page?

Https://msdn.microsoft.com/zh-cn/library/ms178472.aspx

The following article hopes to be inspired.

Asp. NET aspx page runat= the nature of "server" (Essensial of runat= "Server" in ASP. NET)

Today, my colleague asked me a "magical" question, and another colleague "magically" identified the problem but could not explain it, blaming it on a "habit" or "next note". Now I'll describe the problem and do some explaining.

My colleague first added an ASPX page to the existing project and then copied some of the content to the new page from the source of the existing execution of the correct page, but inadvertently left the runat= "server" tag. The specific code is restored as follows:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "webappheadrunatserver._ Default "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "Server" >
String str = "MyPath";
</script>

<title>
<%= "My title"%></title>
<link rel= "Stylesheet" href= "<%= str%>/a.css"/>

<script language= "javascript" type= "Text/javascript" src= "<%=str%>/a.js" ></script>

<body>
</body>

1, the above code will have what strange phenomenon?

In the absence of problems in the code, we always do not care about the existence of the runat= "server", even when the code is a problem, we rarely pay attention to runat= "server" problem, so when the problem arises when we always use "experience" or "try" to solve, It is seldom know everything to know it, because it does look too "small".

So what's so weird about the code above? Because my friend has never noticed runat= "Server", we are always comparing two text differences when the following problem occurs.

My Title
</title><link rel= "Stylesheet" href= "&lt;%= str%>

<script language= "javascript" type= "Text/javascript" src= "mypath/a.js" ></script>

At first, we thought we were wrong, we repeated the <%=str%> copy of the correct position to the wrong place, but not as we would like to, using the breakpoint debugging, we followed the value of STR is correct, but the follow-up action we can not track, so we began to believe that "weird", of course, Advocating science, we also know that this is only temporarily unable to solve.

2, why in debugging, only the href in the link will be a problem, and others will not?

When we repeated 3-4 times, we began to doubt our character, because my colleague said that the code was copied from another page, so the naked eye added the facts to tell us that this is a special case, until we ask another colleague to come over forensics, we decided to compare the "how the code was copied", The original code is a local copy, the main culprit of the code is the head of the runat= "server". Well, the problem was found, but the colleague who found the problem could not explain it, for the only reason was that it was not possible, and we all had to get rid of it. It's really far-fetched, can't we explain? Of course, ask for yourself, and think about it is easy to have results.

3, runat= "Server" when in the end what happened?

Since the problem is caused by the runat= "Server", you have to start with it. runat= the "server" tag, which is designed to identify how the HTML on our page should be interpreted when the ASPX page is compiled. To be precise, the ASPX page is generated so that the ASPX page is read into the compiler, and when an HTML tag does not contain runat= "server", it will be exported as a string or compiled into a new LiteralControl ("specific text");

When encountering runat= "Server", the label will be treated as a htmlcontrol, of course, first of all, this htmlcontrol must exist, the following table shows the HTML tag that can be set runat= "Server", otherwise it will appear "Parser Error" (Parser error message: The path has illegal characters.) ):

Like a

will be resolved to:

HtmlHead head = new HtmlHead ("Head");
HTMLTitle title = This.__buildcontrol__control3 ();
IParserAccessor accessor = head;
Accessor. AddParsedSubObject (title);

Here the AddParsedSubObject is roughly the following code:

public class Control
{
protected virtual void AddParsedSubObject (Object obj)
{
Control child = obj as Control;
if (child! = null)
{
This. Controls.Add (child);
}
}
}

That is the Controls.Add method.

Here are some of the logic for HTML rendering by adding output delegates:

Private HTMLTitle __buildcontrol__control3 ()
{
HTMLTitle title = new HTMLTitle ();
Title. Setrendermethoddelegate (New Rendermethod (THIS.__RENDER__CONTROL3));
return title;
}
private void __render__control3 (HtmlTextWriter __w, Control Parametercontainer)
{
__w.write ("\ r \ n");
__w.write ("My title");
}

4. Why is the href of link wrong and script not?

Generate script

private void __render__control2 (HtmlTextWriter __w, Control Parametercontainer)
{
Parametercontainer.controls[0]. RenderControl (__W);
PARAMETERCONTAINER.CONTROLS[1]. RenderControl (__W);
__w.write ("\r\n\r\n <script language=\" javascript\ "type=\" text/javascript\ "src=\" ");
__w.write (THIS.STR);
__w.write ("/a.js\" ></script>\r\n\r\n ");
PARAMETERCONTAINER.CONTROLS[2]. RenderControl (__W);
}

Generate Htmllink
Private Htmllink __buildcontrol__control4 ()
{
Htmllink link = new Htmllink ();
((iattributeaccessor) link). SetAttribute ("rel", "Stylesheet");
Link. Href = "<%= str%>/a.css";
return link;
}

Obviously, Link's href is to directly assign the text on the page, so the value in STR is not used instead of the text, and in script, str is directly output, so script is possible, and link will be problematic.

5, remove runat= "head" then solve the problem, why?

Because the link tag will no longer be converted to Htmllink after runat= "Head" is removed, link will not be called. The href attribute goes to assign a value, so there is no error.

private void __render__control1 (HtmlTextWriter __w, Control Parametercontainer)
{
__w.write ("\r\n\r\n<! DOCTYPE HTML public \ "-//W3C//DTD XHTML 1.0 transitional//en\" \ "http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd\ ">\r\n__w.write ("\r\n\r\n__w.write ("My title");
__w.write ("</title>\r\n <link rel=\" stylesheet\ "href=\" ");
__w.write (THIS.STR);
__w.write ("/a.css\"/>\r\n\r\n <script language=\ "javascript\" type=\ "text/javascript\" src=\ "");
__w.write (THIS.STR);
__w.write ("/a.js\" ></script>\r\n\r\n ");
Parametercontainer.controls[0]. RenderControl (__W);
__w.write ("\r\nPARAMETERCONTAINER.CONTROLS[1]. RenderControl (__W);
__w.write ("\r\n</body>\r\n}

6. What are the characteristics? Conclusion

Precisely, because under runat= "Server", the compiler will convert all of the xxxx=yyyy parts of all the properties first to its attribute, which is usually set up in the following two ways

A, through the setattribute ("xxxx", "yyyy");

b, if the HTMLControl contains this attribute, it is set by its properties, such as head. href= "yyyy";

Note that here regardless of whether the yyyy is <%=MMM%>, the same will be directly used as text output.

However, what appears in the body will be output in the __w.write (THIS.STR) mode.

    <form id= "Form1" runat= "Server" accept= "<%=str%>" >
<div>
<%=str%>
</div>
</form>

conclusion : In the runat= "Server" under the label, if it can be converted to HTMLControl, then its attribute will not be used in <%=str%> output, if not converted to HTMLControl, There is no specific requirement. If you must use the <%=str%> method, you need to remove it and the runat= "server" on its ancestor node. However, whether a nested structure will be automatically promoted to runat= "Server" is based on standards. For example, if you put the link tag in head and set head to runat= "server", link will be converted to Htmllink, but placing it in <form runat= "Server" > will only be treated as text output. The controls under form do not automatically ascend, such as <form runat= "server" ><input type= "button"/></form> then the button will continue to be output as text, encountering <%=str%> will be converted to __w.write (str);. If you need to promote it to a HtmlButton control, the display specifies that it is <input runat= "server" type= "button"/>.

Compile the following code and open the corresponding assembly under the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP. NET Files\ path, and view the source code with reflector anti-compilation , and verify the above laws.

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "webappheadrunatserver._ Default "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "Server" >
String str = "MyPath";
</script>

<title>
<%= "My title"%></title>
<link rel= "stylesheet<%= str%>" href= "<%= str%>/a.css"/>
<input id= "Button1" type= "button" value= "<%=str%>"/>

<script language= "javascript" type= "Text/javascript" src= "<%=str%>/a.js" ><%=str%><link rel= " stylesheet<%= str%> "href=" <%= str%>/a.css "/></script>

<meta content= "volnet.cnblogs.com" name= "<%=str%>-blog" runat= "Server"/>
<body>
<form id= "Form1" runat= "Server" accept= "<%=str%>" >
<link rel= "stylesheet<%= str%>" href= "<%= str%>/a.css"/>
<div id= "<%=str%>x" >
accept= "<%=str%>"
</div>
<input type= "button" value= "<%=str%>"/>
<input type= "button" runat= "Server" value= "<%=str%>"/>
</form>
</body>

Antique---ASP. aspx page runat= "Server"

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.