How WebForm runs

Source: Internet
Author: User

First, WebForm contains two page files: The aspx front-end page and the cs back-end page file. Through the anti-compiler Reflector, we can see that two different classes are generated on the front-end page and the back-end page in the Dll assembly, And the aspx class on the front-end page inherits from the CS class on the back-end page.
 

The following small login example is the most used:

In the body module of the aspx front-end page, enter:

<Span style = "font-size: 18px;"> <body>
 
<Form id = "form1" runat = "server">
 
Username: <input type = "text" id = "txtName" runat = "server"/> <br/>
 
Password: <input type = "text" id = "txtPwd" runat = "server"/> <br/>
 
<Input type = "submit" value = "login"/>
 
<Input type = "reset" value = "cancel"/>
 
</Form>
 
</Body>
 
</Span>
<Span style = "font-size: 18px;"> <body>

<Form id = "form1" runat = "server">

Username: <input type = "text" id = "txtName" runat = "server"/> <br/>

Password: <input type = "text" id = "txtPwd" runat = "server"/> <br/>

<Input type = "submit" value = "login"/>

<Input type = "reset" value = "cancel"/>

</Form>

</Body>

</Span>
 

On the cs background page, click this in the current application to display the tag controls with IDs txtName and txtPwd on the aspx page and obtain their values:

String name = this.txt Name. Value;

String pwd = this.txt Pwd. Value;

 

Problem:

1. Why can cs call controls on the aspx page?

2. Can any control in aspx be called by cs?

 

To solve the above two problems, we must first understand the operating principles of ASP.net as a whole:

 

As shown in, the simplified steps for a browser to request An aspx page from the server are as follows:

A. The server will first create the aspx class object on the foreground page, because we have seen the inheritance of the foreground page class and the background page class through the Anti-compiler, so when the child class (aspx class) when created, the parent class (CS class on the background page) will also be created.

B. The ProcessRequest method will be called in the foreground Page class (the PR method is defined in the Page class instead of in the foreground Page class, because the CS class inherits from the page class, And the aspx class inherits from the page class, the PR method is equivalent to defined in the grandpa class of the aspx class ).

C. Call the BuildControlTree method in the PR method, convert all html Controls on the front-end page and runat = server Controls into corresponding control objects and add them to the Controls set of the front-end page class (here the current page is the aspx page class is the root node), in addition, the control object of runat = server is saved in a corresponding type variable in the CS class in the background.

D. Call the Page_Load method of the CS class on the background page in the PR method. The code in this method is written by the programmer.

E. Finally, call the Render_Controls method in the PR method to traverse the Render_Controls method of each node in the control tree and generate the complete html code.

F. Return the complete html code to the browser.

Www.2cto.com

Through the simple description above, we can easily get the answers to the two questions saved above:

1. cs can call controls on the aspx page because the BuildControlTree method is called before calling the Page_Load method of the cs page class in the background, convert the html control and runat = "server" control on the front-end aspx page to the corresponding control object, and save it in the Control set in the aspx page class (the current page aspx page class is also stored as the root node in the controls set). Finally, the page Control tree object is generated.

2. not all html controls in the front-end aspx page class can be called in cs, but only controls contained in the <form runat = "server"> </form> label, this control also contains the runat = "server" property to ensure that the corresponding type of variables exist in the cs class in the background when the control tree is generated, and serves as a member of cs.

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.