Wednesday 28 November 2012

Sharepoint Web part life Cycle



Sharepoint Web part life Cycle:

 Below is the steps for life cycle of a web part.

OnInit: This method handles initialization of the control.
protected override void OnInit(EventArgs e)
{
 
}


OnLoad: This event handles the Load event. This is also used for initialize the control but is not intended for loading data or other processing functionality.
protected override void OnLoad(EventArgs e)
{
}

CreateChildControls
  • This is the most popular event in web part life cycle.
  • This creates any child controls. So if you are adding any control to display then you have to write in this method.
  • Event used to set properties and default values to controls collection
  • If the page is being rendered for the first time the method generally occurs after the OnLoad event.
  • If it is Postback, It is called before OnLoad event.
In this routine control properties and methods previously declared are defined. In most cases, we have to initialize the control's default value (such as Text, Checked, Items, and so on) and activity that is possible to call just at first WebPart load, checking PostBack. The controls specified are created and added to the controls collection. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.
protected override void CreateChildControls()
{
}
OnPreRender
  • Event used to set up business logic related work and in this routine controls value kept by viewstate
  • Here we can change any of the web part properties before the control output is drawn. This is the routine where the control's value is kept for the View State.
protected override void OnPreRender(EventArgs e)
{
}
Render
  • HTML Output is generated.
  • Event creates the HTML that appears in the body of the Web Part.
  • The CreateChildControls method is used to add controls to the WebPart, and the RenderContents method is used to tell the page framework how to render the control into HTML to display on a page.
     
protected override void Render(HtmlTextWriter writer)
{
}
OnUnload
This is executed when the web part is unloaded.
protected override void OnUnload(EventArgs e)
{
}

Dispose
This to free the memory.
public override void Dispose()
{
}

Please correct me in case i am wrong in any of the step.

3 comments: