Saturday, January 12, 2013

Web Part Life Cycle

Simple Webpart Life Cycle (Not UserControl based WebPart).

 
  1. OnInit
  2. OnLoad
  3. LoadViewState 
  4. CreateChildControls
  5. EnsureChildControls
  6. OnPreRender
  7. SaveViewState
  8. Render
  9. RenderContents
  10. OnUnload
  11. Dispose

Visual Webpart Life Cycle ( UserControl based WebPart).

  1. OnInit
  2. LoadViewState
  3. OnLoad
  4. Page_Load
  5. OnPreRender
  6. SaveViewState
  7. Render
  8. OnUnload
  9. Dispose
There are two way the events are firing.

Initial Load:
Events fired when a web part is initially loaded.
OnInit
OnLoad
EnsureChildControls
CreateChildControls
EnsureChildControls
CreateControlCollection
EnsureChildControls (many)
OnPreRender
EnsureChildControls (many)
SaveViewState
RenderControl
Render
RenderControl
RenderContents
RenderChildren
OnUnload
Dispose
Post-back:
  • OnInit
  • LoadViewState
  • EnsureChildControls
  • CreateChildControls
  • EnsureChildControls
  • CreateControlCollection
  • EnsureChildControls (many)
  • OnLoad
  • btnUpdate_Click
  • OnBubbleEvent
  • EnsureChildControls
  • OnPreRender
  • EnsureChildControls (many)
  • SaveViewState
  • RenderControl
  • Render
  • RenderControl
  • RenderContents
  • RenderChildren
  • OnUnload
  • Dispose
 
OnInit: This method handles initialization of the control.
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.
LoadViewState: The view state of the web part is populated over here.
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.
EnsureChildControls: This method ensures that CreateChildControls has executed. EnsureChildControls method must be called to prevent null reference exceptions.
SaveViewState: View state of the web part saved.
OnPreRender: This method handles or initiates tasks such as data loading that must complete before the control can render.
Page.PreRenderComplete: The page fires the PreRenderComplete event after all controls have completed their OnPreRender methods.
Render: This method is used to render everything.
RenderContents: Renders the contents of the control only, inside of the outer tags and style properties.
OnUnload: Performs the final cleanup.

No comments:

Post a Comment