Introduction to ASP. NET internal mechanism (V)
This chapter introduces the page lifecycle, which involves many processes, but it is not difficult at all. Believe it or not. I try my best to be honest, and understanding the lifecycle of a page is helpful to anyone who likes to develop custom controls and components.
SeriesArticleLink:
Introduction to the internal mechanism of ASP. NET (I)
Introduction to the internal mechanism of ASP. NET (2)
Introduction to ASP. NET internal mechanism (III)
Introduction to ASP. NET internal mechanism (IV)
Introduction to ASP. NET internal mechanism (V)
Introduction to ASP. NET internal mechanism (6)
Introduction to ASP. NET internal mechanism (7)
Introduction to ASP. NET internal mechanism (8)
The page lifecycle, that is, the process of processing a page request. As we have mentioned before, different files are processed differently.Program. And the handler implements the ihttphandler interface. There are two methods in this interface. The processrequest method is used to process the request.
So when we request a page, assume that we request the default page. the ASPX page is inherited from the page. You should know that default is used when the program is running. aspx and its. CSCodeThe part is compiled into default_aspx, as follows:
Code
Using System;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. htmlcontrols;
Namespace ASP
{
Public Class Default_aspx: Page
{
Protected Scriptmanager scriptmanager1;
Protected Textbox textbox1;
Protected Button button1;
Protected Label Info;
Protected Updatepanel updatepanel1;
Protected Htmlform form1;
PrivateUpdatepanel @__ buildcontrolupdatepanel1 ()
{
...
}
Private Htmlform @__ buildcontrolform1 ()
{
...
}
Private Void Buildcontroltree (default_aspx @__ CTRL)
{
Iparseraccessor parser = (Iparseraccessor) (@__ CTRL ));
Parser. addparsedsubobject (
New Literalcontrol (" < HTML xmlns = \ "Http: // Www.w3.org/5o/xhtml#"> "));
Parser. addparsedsubobject ( New Literalcontrol ("\ r \ n < Body > \ R \ n ");
Htmlform ctrl2 = This . @__ Buildcontrolform1 ();
Parser. addparsedsubobject (@__ ctrl2 );
Parser. addparsedsubobject (
New Literalcontrol ("\ r \ n </ Body > \ R \ n </ Html > \ R \ n "));
}
Protected Override VoidFrameworkinitialize ()
{
Base. Frameworkinitialize ();
This. Buildcontroltree (This);
}
}
}
Because our request is the default. aspx page above the request, that is, the requested file ends with. aspx. The processing program that happens to result from the. aspx file is handled by page, because the page class implements ihttphandler. So it is special here, and many people are confused here. Because default_aspx inherits page, the request for defult. aspx is actually processed by the default_aspx class instance. I'm sorry for the point here. Basically, we can think that we are "Dealing with ourselves" (this statement is not strict, but we hope you can understand it more deeply ).
In fact, the processing process is the so-called page lifecycle. To put it bluntly, it is the processing process, let's take a look at the processrequest method of the following page class. (You just need to scan the following code)
Code
Public Void Processrequest (httpcontext context)
{
This . _ Context = Context;
This. Retrieveposteddata ();
If ( This . Maintainscrollpositiononpostback)
This . Loadscrollposition ();
This . Performpreinit ();
This . Initrecursive ( Null );
This . Oninitcomplete (eventargs. Empty );
If ( This . Ispostback)
{
This . Loadallstate ();
This . Processpostdata ( This . _ Requestvaluecollection, True );
}
This . Onpreload (eventargs. Empty );
This . Loadrecursive ();
If ( This . Ispostback)
{
This . Processpostdata ( This . _ Leftoverpostdata, False );
This . Raisechangedevents ();
This . Raisepostbackevent This . _ Requestvaluecollection );
}
This . Onloadcomplete (eventargs. Empty );
This . Prerenderrecursive ();
This . Performprerendercomplete ();
This . Saveallstate ();
This . Onsavestatecomplete (eventargs. Empty );
This . Rendercontrol ( This . Createhtmltextwriter ( This . Response. Output ));
}
The above code is not familiar to us, such as the init event trigger method and the load event trigger method (both developed on ). I believe you should understand that the lifecycle of a page is actually a process of calling methods one by one.
The following is an illustration of method execution:
You can refer to the method as described in the following sections. I will not go into details about the functions of each method here.
Here we are today. Thank you!