ASP.NET page life cycle diagram

ASP.NET page life cycle diagram
October 27, 2010 Wednesday 10:39

ASP.NET programming model of ASP.NET page life cycle What are they? What does it include? Programming model of ASP.NET ASP.NET page life cycle specific process, what does? Here we begin to explain it:

ASP.NET page runs, the page will go through a life cycle, a series of processing steps in the life cycle. These steps include initialization, instantiating controls, to restore and maintain the state, running event handler code, and rendering. Understand the page life cycle is very important, so that we can write code in the appropriate stage of the life cycle, in order to achieve the desired results. In addition, if you develop a custom control, you must be familiar with the page life cycle, so as to properly initialize the controls, the use of view state data to populate control properties and behavior logic operation of all controls.

Page ASP.NET programming model of the life cycle of a series of steps: page initialization, instantiating controls, to restore and maintain the state, running event handler code to display. In order to perform the necessary code at the appropriate stage, so be very familiar with the page life cycle. At all stages of the life cycle of the page, the page-by-defined trigger event, by executing the program we need in life event page in the development of the code
page life cycle stages

1, ASP.NET page life cycle of page requests: occurs before the page life cycle, when the user requests a page, ASP.NET will determine the need for analysis and compilation pages to determine whether to start the life cycle of the page, or if you can not send page cache to respond in the case of running page.

2, the beginning of the life cycle of an ASP.NET page: Set the page properties, such as: HttpContext and other properties; at this stage, to determine the required page is postback request or a new request and set IsPostBack property; UICulture property settings page.

3, page ASP.NET page life cycle of initialization: Load all subjects; the controls generation and set UniqueID;

Note: ViewState, the value has not yet been loaded into ControlState in control; if the page is a postback, the postback data have not been loaded; it controls can be accessed at this time, but the value may be wrong.

4, load the ASP.NET page life cycle: if the current request is a postback request, for the control loads ViewState value and ControlState in.

5, ASP.NET page lifecycle of authentication: calling all validator controls the Validate method, which sets the IsValid property validator controls and pages.

6, postback events ASP.NET page life cycle of the process: If the request is a postback request, all event handler is called.

7, ASP.NET page life cycle of presentation: First of all controls on the page and save the view state, and then call the Render method for each control, it will provide a text writer, writes for the input controls page OutputStream Response property.

8, ASP.NET page life cycle of unloading: finish rendering, and has been sent to the client page, ready to discard the page after the call to unload. The uninstall attributes such as: Response and Request, and so on.

Here is the ASP.NET programming model of ASP.NET page life cycle diagram

ASP.NET page life cycle diagram 

The following is the initial page ASP.NET process:
1. the Page_Init ();
2. the Load the ViewState;
3. a Postback the Load Data;
4. the Page_Load ();
5. The Control the Handle Events;
6. The the Page_PreRender ();
7. The Page_Render () ;
8. The Event the Unload;
9. The Called the Dispose Method;

下面对其中的一些过程作下描述:
1. Page_Init();
这个过程主要是初始化控件,每次页面载入执行这个初始过程,包括第一次和以后的Postback(这里说下Postback,其实就可以简单理解成用户点
击SUBMIT按钮之类的,把表单<Form>提交给服务器,这就是一次postback),在这里面可以访问控件,但是这里面的控件值不是我们期待的控件里面的值,他只是一个控件的初始值(默认值),举例: 比如一个TextBox1,我们填入了"哈哈",在点击SUBMIT提交了页面后,在Page_Init()里面,我们访问到的TextBox1.Text不是我们的"哈哈",而是开始的""空字符串,如果TextBox1在我们设计的时候提供了默认值,这里访问到的也就是提供的默认值,为什么呢,这就要看下一个过程了.

对应的事件Page.Init

2. Load ViewState
这个过程是载入VIEWSTATE和Postback数据,比如我们上面的TextBox1,这时就赋了"哈哈",所以,在Post_Init()对控件赋值是无意义的,它都会
在这个过程里被改写,当然第一次页面载入例外,因为没有VIEWSTATE数据。

没有对应的事件

3.Load Postback data;
上面说了,Postback可以理解成用户提交表单数据,所以这里就是处理表单数据,当然这里要设计到控件的设计,一般情况不会要我们自己处理这
个过程,我们暂且略过. (在以前那篇关于ASP.NET页面生命周期的简单描述中,把这个过程和Load ViewState放在了一起,其实那是微软提供的生命周期过程,这里单独提出来是为了让大家明白这是一个单独的过程)

没有对应的事件
4. Page_Load();
这个过程也是每次页面载入时一定会执行的,但是注意和Page_Init的区别,上面已经涉及了,这里注意的是一般都会用到Page.IsPostBack,该
值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问。
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
   //第一次执行的CODE HERE
}
else
{
//用户提交FORM(即Postback)CODE HERE
  }

  //每次这里的都回执行CODE HERE
}

对应的事件Page.Load

5. Handle control events;
这个过程里,相应具体的控件事件,比如private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)事件等等

没有对应的事件(我们自己的事件函数都包括在这个过程里比如上面的ListBox1_SelectedIndexChanged)

6. Page_
预先呈递对象,这里是在向用户程序呈现数据的倒数第二步,我估计提供这个过程的意义,也就是在这里能对控件属性等等要呈现给用户的数据进
行修改,这也是最后的修改,以前的修改(比如在Page_Init里面)都可能被覆盖.做完这了还会进行一个操作就是保存状态,即SaveViewState.

对应的事件时Page.PreRender

7. Page_Render();
大家可以在浏缆器里View->Source查看到,每个页面都有一个隐藏的<input>,这里面的"__VIEWSTATE"就是我们服务器写回来的页面状态信息,
在 这个之前,服务器要呈现页面(也就是构造HTML格式的文件),就是从这个"__VIEWSTATE"里面获取的数据,当然大家也注意到了,这里有个 Page.Render事件,我们可以添加自己的处理代码,也就是说我们又可以更改数据,不过这里推荐不要在这里修改,既然提供了PreRender,就 应该在里面做最后的修改,当然这不是必须的,只是推荐!

对应的事件Page.Render

8. Unload event;
大家应该明白,当想服务器请求一个对象的时候,就会在内存里生成一个继承页面对象,也就是页面的类,它继承自System.Web.UI.Page.
当页面对象从内存中卸载时发生,将触发该事件.

对应的事件Page.Unload

9. Dispose method called;
销毁所有的对象.当从内存释放Page时发生,这是生存期的最后阶段。可能第8和9似乎有些模糊,不过我也没怎么搞清楚,待研究!

对应的事件Dispose

以上就是ASP.NET页面周期的描述。

注意上面灰色背景的文字,如果一个过程中有对应的事件,我们可以自己定义一个函数(当然先在MSDN中找到函数原型),然后在
InitializeComponent中向事件的链表上添加上去,像下面:
private void InitializeComponent()
{    
this.Unload += new System.EventHandler(this.MainWebForm_Unload);
this.Load += new System.EventHandler(this.Page_Load);
this.Init += new System.EventHandler(this.Page_Init);
this.PreRender += new System.EventHandler(this.My_PreRender);
}

Several processes for no corresponding event, such as 2.Load ViewState, we can override Page virtual function the override void the LoadViewState protected (Object savedState); add their own control codes, but should not fall with the corresponding base class, such as :
protected void the override  the LoadViewState ( Object  savedState)
{
// own processing the VIEWSTATE
Base .LoadViewState (savedState);
}


Reproduced in: https: //www.cnblogs.com/Tim_Liu/archive/2011/04/26/2029507.html

Guess you like

Origin blog.csdn.net/weixin_34367845/article/details/94724556