您的位置:首页 > 移动开发

ASP.NET application and page life cycle

2010-05-11 18:19 861 查看
转载自CodeProject,原文地址:http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx#Introduction

Introduction
The Two step process
Creation of ASP.NET environment
Process request using MHPM events fired
In What event we should do what?
A sample code for demonstration
Zooming ASP.NET page events
References

Introduction

In this article we will try to understand what are the different events which takes place right from the time the user sends a request, until the time request is rendered on the browser. So we will first try to understand the two broader steps of an ASP.NET request and then we will move in to different events emitted from ‘HttpHandler’, ‘HttpModule’ and ASP.NET page object. As we move in this event journey we will try to understand what kind of logic should go in each every of these events.
This is a small Ebook for all my .NET friends which covers topics like WCF,WPF,WWF,Ajax,Core .NET,SQL etc you can download the same from Click here or else you can catch me on my daily free training @ Click here

The Two step process
From 30,000 feet level ASP.NET request processing is a 2 step process as shown below. User sends a request to the IIS:-
• ASP.NET creates an environment which can process the request. In other words it creates the application object, request, response and context objects to process the request.
• Once the environment is created the request is processed through series of events which is processed by using modules, handlers and page objects. To keep it short lets name this step as MHPM (Module, handler, page and Module event), we will come to details later.
代码

public partial class _Default :System.Web.UI.Page
{
protected void Page_init(object sender, EventArgs e)
{

clsHttpModule.objArrayList.Add("Page:Init");
}
protected void Page_Load(object sender, EventArgs e)
{
clsHttpModule.objArrayList.Add("Page:Load");
}
public override void Validate()
{
clsHttpModule.objArrayList.Add("Page:Validate");
}
protected void Button1_Click(object sender, EventArgs e)
{
clsHttpModule.objArrayList.Add("Page:Event");
}
protected override void Render(HtmlTextWriter output)
{
clsHttpModule.objArrayList.Add("Page:Render");
base.Render(output);
}
protected void Page_Unload(object sender, EventArgs e)
{
clsHttpModule.objArrayList.Add("Page:UnLoad");
}
}
Below is how the display looks like with all events as per the sequence discussed in the previous section.



Zooming ASP.NET page events
In the above section we have seen the overall flow of events for an ASP.NET page request. One of the most important section is the ASP.NET page, we have not discussed the same in detail. So let’s take some luxury to describe the ASP.NET page events in more detail in this section.
Any ASP.NET page has 2 parts one is the page which is displayed on the browser which has HTML tags , hidden values in form of viewstate and data on the HTML inputs. When the page is posted these HTML tags are created in to ASP.NET controls with viewstate and form data tied up together on the server. Once you get these full server controls on the behind code you can execute and write your own login on the same and render the page back to the browser.



Now between these HTML controls coming live on the server as ASP.NET controls, the ASP.NET page emits out lot of events which can be consumed to inject logic. Depending on what task / logic you want to perform we need to put these logics appropriately in those events.
Note: - Most of the developers directly use the ‘page_load’ method for everything, which is not a good thought. So it’s either populating the controls, setting view state, applying themes etc everything happens on the page load. So if we can put logic in proper events as per the nature of the logic that would really make your code clean.

SeqEventsControls InitializedView state

Available

Form data
Available
What Logic can be written here?
1InitNoNoNoNote: - You can access form data etc by using ASP.NET request objects but not by Server controls.Creating controls dynamically, in case you have controls to be created on runtime. Any setting initialization.Master pages and them settings.In this section we do not have access to viewstate , posted values and neither the controls are initialized.

2Load view stateNot guaranteedYesNot guaranteed You can access view state and any synch logic where you want viewstate to be pushed to behind code variables can be done here.
3PostBackdataNot guaranteedYesYesYou can access form data. Any logic where you want the form data to be pushed to behind code variables can be done here.
4LoadYesYesYesThis is the place where you will put any logic you want to operate on the controls. Like flourishing a combox box from the database , sorting data on a grid etc. In this event we get access to all controls , viewstate and their posted values.
5ValidateYesYesYesIf your page has validators or you want execute validation for your page this is the right place to the same.
6EventYesYesYesIf this is a post back by a button click or a dropdown change then the relative events will be fired. Any kind of logic which is related to that event can be executed here.
7Pre-renderYesYesYesIf you want to make final changes to the UI objects like changing tree structure or property values, before these controls are saved in to view state.
8Save view stateYesYesYesOnce all changes to server controls are done this event can be an opportunity to save control data in to view state.
9RenderYesYesYesIf you want to add some custom HTML to the output this is the place you can.
10UnloadYesYesYesAny kind of clean up you would like to do here.


References

I am not so smart to write this article by myself ;-) , lot of things I have plugged from the below articles.
Read more about IIS 7.0 life cycle http://msdn.microsoft.com/en-us/library/bb470252.aspx
Intercepting filters http://msdn.microsoft.com/en-us/library/ms998536.aspx
Explains how to implement Httphandlers and modules http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx
Httphandlers and Httpmodules :- http://www.15seconds.com/Issue/020417.htm
Implementing security using modules and handlers http://joel.net/articles/asp.net2_security.aspx
Difference between Httpapplication and global.asax http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: