Geeks With Blogs
SharePoint Wiki! Sharing is gaining...

I just came across a strange issue with 'Connected Webparts' where in the parameter/value being passed to the user webpart was being defaulted to the first value/initial set up value. So for instance it is the user id being passed through a custom webpart to a report viewer. So what was happening was that the user id was always set/defaulted to the user-id of the user who added the webpart. On more reading and researching I found out that it was due to the order of events. I was using the createchildcontrols method which never updated the user-id everytime a new user logged in. The easy solution was to just replace it with the OnInit function but this prompted me to write more about the webpart life cycle. 

It is not very different from a asp.net web-page life cycle, but it's important we know everything about these events and the sequence to avoid dev blunders :)

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

 

Like Asp.Net life cycle there is also Web Part life cycle. So it is better to understand the web part life cycle. I’ve explained some of them which are used by every developer to cover most of the requirements:

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.

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.

Posted on Wednesday, July 18, 2012 10:29 AM | Back to top


Comments on this post: SharePoint WebPart Life-Cycle Events and Event Sequence

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © KunaalKapoor | Powered by: GeeksWithBlogs.net