This is a discussion on What are the cycle of methods involved when a page loads in dot net 2005? within the ASP and ASP.NET Programming forums, part of the Web Development category; What are the cycle of methods involved when a page loads in dot net 2005?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| It is very important to know that for each request, the Page class is instantiated everytime from “scratch”. Which means that any values or whatever state it had previously will get lost unless we use one of the various state maintainance mechanisms provided by ASP.NET like Application, Session, Cache variables or Cookies. 1. PreInit() In this Page level event, all controls created during design time are initialized with their default values. For e.g., if you have a TextBox control with Text property = “Hello”, it would be set by now. We can create dynamic controls here. This event occurs only for the Page class and UserControls/MasterPages do not have this method to override. 2. OnInit() In this event, we can read the controls properties (set at design time). We cannot read control values changed by the user because that changed value will get loaded after LoadPostData() event fires. But we can access control values from the forms POST data as: string selectedValue = Request.Form[controlID].ToString(); 3. LoadViewState This will only fire if the Page has posted back (IsPostBack == true). Here the runtime de-serializes the view state data from the hidden form element and loads all controls who have view state enabled. 4. LoadPostBackData Again, this method will only fire if the Page has posted back. In this event the controls which implement IPostBackDataHandler interface gets loaded by the values from the HTTP POST data. Note that a textbox control does not gets its value from the view state but from the post data in the form in this event. So even if you disable view state for a particular control, it can get its value from the HTTP POST data if it implements IPostBackDataHandler interface. Also, an important point to note is that if we have a DropDownList control and we have dynamically added some items to it, the runtime cannot load those values unless the view state is enabled (even if the control derives from IPostBackDataHandler). The reason being that HTTP Post data has only one value per control, and the entire value collection is not maintained in the PostData but in view state. 5. Page_Load This is the most popular method and the first one for all beginner developers to put their code. Beginners may also think that this is the first method which fires for a Page class. This can lead to a lot of confusion which makes understanding the Page lifecycle all the more important. Note: If the page has any user control, then it's Load method will fire after the Page class's Load method. The reason as explained earlier is the fact that all method except the Init() are fired from the outermost control to the innermost. So after Page_Load(), load methods of all other controls are fired recursively. 6. Control Event Handlers These are basically event handlers (like Button1_Click()) which are defined for controls in the ASPX markup. Another source of confusion arises when the developer thinks that an event handler like Button_Click() should fire independently (like in windows apps) as soon as he clicks a Button on the web form, forgetting that Page_Load will fire first before any event handlers. 7. PreRender This event is again recursively fired for each child controls in the Page. If we want to make any changes to control values, this is the last event we have to peform the same. 8. SaveViewState Here, the ViewState of the controls gets saved in the form's hidden control. 9. Render In this method all controls are rendered recursively (i.e. Render method of each control is called). 10. Unload Here you can have the page and controls perform clean-up operations. This event has no relevance besides clean up operations because the Page has already rendered. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 'Page' ia an unambiguous reference between 'System.Web.UI.Page' and 'Project1.Page' | poornima | ASP and ASP.NET Programming | 1 | 03-05-2008 03:12 AM |
| RMI and steps involved in developing an RMI object | vijayanand | Java Programming | 0 | 09-20-2007 08:06 AM |
| How can i maintain page scroll position after a postback ASP.NET 2005? | a.deeban | ASP and ASP.NET Programming | 1 | 08-22-2007 10:24 PM |
| When during the page processing cycle is ViewState available? | prasath | ASP and ASP.NET Programming | 1 | 07-19-2007 03:29 AM |
| What methods are fired during the page load? | prasath | ASP and ASP.NET Programming | 1 | 07-18-2007 03:04 AM |