Monday, 16 December 2013

ASP.NET Page Life Cycle Events -2

ASP.NET Page Life Cycle Events

1.PreInit
  
protected void Page_PreInit(object sender, EventArgs e)   
{       
  • Use this event for the following:         
  • Check the IsPostBack property to determine whether this is the first time the page is being processed.       
  • Create or re-create dynamic controls.       
  • Set a master page dynamically.       
  • Set the Theme property dynamically.          
}

2.Init
protected void Page_Init(object sender, EventArgs e)
{
  • Raised after all controls have been initialized and any skin settings have been applied.
  • Use this event to read or initialize control properties.
}
3.InitComplete

protected void Page_InitComplete(object sender, EventArgs e)
{      
  • Raised by the  Page object. Use this event for processing tasks that require all initialization be complete.
}
4.PreLoad
 (1)Loads ViewState : ViewState data are loaded to controls
 (2)Loads Postback data : postback data are now handed to the page controls            
protected override void OnPreLoad(EventArgs e)
{       
  • Use this event if you need to perform processing on your page or control before the  Load event.       
  • Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
}
5.Load

protected void Page_Load(object sender, EventArgs e)
{       
  • The  Page calls the  OnLoad event method on the  Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.      
  • Use the OnLoad event method to set properties in controls and establish database connections.
}
6.Control (PostBack) event(s)ASP.NET now calls any events on the page or its controls that caused the PostBack to occur. This might be a button’s click event or a dropdown's selectedindexchange event, for example. These are the events, the code for which is written in your code-behind class(.cs file).

protected void Button1_Click(object sender, EventArgs e)
{       
  • This is just an example of control event.. Here it is button click event that caused the postback
}
7.LoadComplete

protected void Page_LoadComplete(object sender, EventArgs e)
{       
  • Use this event for tasks that require that all other controls on the page be loaded.
}
8.PreRender

protected override void OnPreRender(EventArgs e)
{       
  • Each data bound control whose DataSourceID property is set calls its DataBind method.       
  • The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
}
9.SaveStateComplete

protected override void OnSaveStateComplete(EventArgs e)
{       
  • Before this event occurs,  ViewState has been saved for the page and for all controls. Any changes to the page or controls at this point will be ignored.       
  • Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
}
10.Render

  • Render stage goes here. This is not an event
11.UnLoad 

protected void Page_UnLoad(object sender, EventArgs e)   
{       
  • This event occurs for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.       
  • During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream.          
  • If you attempt to call a method such as the Response. Write method, the page will throw an exception.   
}
 

No comments:

Post a Comment