These two days are watching "ASP.NET Server Controls and Components Development" to solve a lot of confusion.
A big reason from ASP to ASP.NET is the easy-to-use, automatic status of the server control, and the convenient event registration, but did not understand how it is achieved. Because the HTTP request is stateless, we need to save the value to be saved in the hidden domain or put into session or cookies in the hidden domain. In .NET, the page framework is actually a process of simulating a stateful similar desktop program. Each subsequent request is restored in the previous request interrupt and proceeds to execute. This simulation is achieved by the following logical phases.
Take a TextBox as an example (other controls or even the page and it almost), when requesting the page, if it is included in the control tree to be generated, it will be called by the page or other controls. The device is instantiated. The generation of the control tree is also very interesting. All the controls from the Control class (the page is also indirectly derived from control), all have heavy loaded render and renderchildren methods and renderControl (to overload these methods when writing, join your own Processing logic), page control first creates an instance of HTMLTEXTWRITER, then pass this object to the renderControl method, RenderControl method checks the Visible property of the control, if true, call the render method, render method default implementation will call the RenderChildren method And this method will call the RenderControl method for each sub-control by default, so that the generation of the control tree is completed by recursive. Below is the code seen from the book.
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 C in Controls) {C.RenderControl (Writer);}}
What is HTMLTEXTWRITER? Check MSDN, you can see them in system.web.ui namespace, which explains "Write a series of continuous HTML specific characters and text on the web form page. This class provides ASP.NET server controls HTML content is presented to the formatting function used by the client. "
Back to the page model, you will call the OnNit method by default when instantiated, and you can overload your initialization logic in this process. Then, the most important step of implementing a stateful web program is the first to automatically call the TRACKVIEWSTATE method, track the changes of the view status, and save it to the STATETAG object of the control. If it is back, the loadViewState method is called to restore the ViewState dictionary, and the iPostBackDataHandler interface is implemented like TextBox, and the control data status will be updated via the loadpostdata method. Referring is loaded, and all the last states of all controls have been retained. The next step If it comes back, the RaisePostDataChangeDevent method is triggered. If not, enter prerender, then save the view status, generate the control (unload), release (Dispose) (release all resources). At this point, the page processing is complete, and the handler will output the generated HTML stream to the client.