Asp.net Page Model

Source: Internet
Author: User
In the past two days, I have read ASP. NET Server Control and component development, which solves a lot of confusion.
A major reason for switching from ASP to ASP. NET is the ease-of-use of server controls, automatic status persistence, and convenient event registration, but I didn't understand how it was implemented at the time. Because the HTTP request is stateless, We need to submit the value to be saved in the hidden domain in ASP or put it into session or cookie. In. net, the page framework is actually simulating a stateful desktop-like Program . Each subsequent request is resumed and continues to be executed where the previous request was interrupted. This simulation relies on the following logical stages.
Take a textbox as an example (other controls and even pages are similar to it). When a request arrives at the page, if it is included in the control tree to be generated, it will be instantiated by the constructor called by the page or other controls. The generation of the Control tree is also very interesting. All controls derived from the control class (the page is also indirectly derived from the control ), there are reloaded render and renderchildren methods and rendercontrol (you can reload these methods when writing a program and add your own processing logic). The page control first creates an htmltextwriter instance, then pass the object to the rendercontrol method. The rendercontrol method checks the visible attribute of the control. If it is true, the render method is called. The default implementation of the render method calls the renderchildren method, by default, this method calls the rendercontrol method of each sub-control. In this way, the control tree is generated through recursion. The following is what we see in the book: Code . Public   Void Rendercontrl (htmltextwriter writer) {
If(Visible)
{Render (writer );}
}
Protected   Virtual   Void Render (htmltextwriter writer)
{
Renderchildren (writer );
}
Protected   Virtual   Void Renderchildren (htmltextwriter writer)
{
Foreach(Control CInControls)
{C. rendercontrol (writer );}
}

what is htmltextwriter? Check msdn and you can see that they are in the system. Web. UI namespace. The explanation is "write a series of consecutive HTML-specific characters and texts on the web form page. This class provides the Formatting Function used by ASP. NET Server controls to render HTML content to the client ".
return to the page model. When each control is instantiated, The oninit method is called by default to trigger the init event. In this process, you can reload the oninit method to add your own initialization logic. Then the most important steps to implement stateful web programs are taken. First, the trackviewstate method is automatically called on the page to track changes to the view status and save the changes to the statetag object of the control. If it is a return request, the loadviewstate method is called to restore the viewstate dictionary. If the ipostbackdatahandler interface is implemented like Textbox, the control data status is updated through the loadpostdata method. The next transfer is load, and all the final states of all controls are retained. In the next step, the raisepostdatachangedevent method is triggered. If not, the method enters the prerender, saves the view status, generates the control (render), unloads (unload ), release (dispose) (release all resources ).
at this point, the page processing is complete, and the processing program will output the generated HTML stream to the client.

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.