Sharepoint WebPart Lifecycle Events


This post describes the lifecycle events of a Sharepoint Connectable WebPart wih a single ViewState backed property;

On Page Load

  1. Constructor
  2. OnInit
  3. OnLoad
  4. ConnectionConsumer method is called if web part is connectable (sets the connection providers interface in the webpart)
  5. CreateChildControls
  6. OnPreRender (if your web part is connectable you would typically call the connection provider here to retrieve data)
  7. SaveViewState
  8. Render
  9. RenderChildren
  10. RenderContents

On 1st Postback
(PostBack click handler sets ViewState via public Property)

  1. Constructor
  2. OnInit
  3. CreateChildControls
  4. OnLoad
  5. PostBack click handling
  6. ConnectionConsumer method is called if web part is connectable (sets the connection providers interface in the webpart)
  7. OnPreRender (if your web part is connectable you would typically call the connection provider here to retrieve data)
  8. SaveViewState
  9. Render
  10. RenderChildren
  11. RenderContents

On 2nd Postback
(PostBack click handler sets ViewState via public Property)

  1. Constructor
  2. OnInit
  3. LoadViewState
  4. CreateChildControls
  5. OnLoad
  6. PostBack click handling
  7. ConnectionConsumer method is called if web part is connectable (sets the connection providers interface in the webpart)
  8. OnPreRender (if your web part is connectable you would typically call the connection provider here to retrieve data)
  9. SaveViewState
  10. Render
  11. RenderChildren
  12. RenderContents

Note that during the 2nd postback, LoadViewState, is called, since in the 1st postback the click handler sets the value of the ViewState backed public property.

About these ads

About Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

16 Responses to “Sharepoint WebPart Lifecycle Events”

  1. Thanks for this post. It has helped me greatly.

  2. I am having a problem when developing a MOSS webpart where am adding a LinkButton in GridView. The LinkButtons will be clicked by the user to download files.

    1) The GridView is getting created in CreateChildControls(). Also the event handlers like OnRowCommand are mapped here.
    2) The GridView is getting populated in OnPreRender(). This is because I only have the ConnectionConsumer value at this level from which I am creating the collection to assign as datasource. So the LinkButtons within the GridView get to be created at this level.
    3) When the LinkButtons are clicked they don’t fire the OnRowCommand event. Whereas Sort/Page fires the OnRowCommand.

    Since the LinkButtons are created only at the OnPreRender level, they are not getting mapped to the parent GridView’s OnRowCommand event.

    When I moved the populating code up in CreateChildControls (using a fixed value for Connection data) the LinkButtons are firing OnRowCommand!

    Any help?

  3. Hi,
    In my case, the LoadViewStae is not getting fired for my Web Part on the click of the LinkButton on the Toolbar and so the viewstate is not getting stored.
    Seems like its not getting fired at all. Only the SaveViewState is getting fired.
    Also what do you mean by Ist Post back and 2nd Postback in the article above ?
    I thought there is either a postback or not !
    What am I missing here ?
    Thank you in advance.

  4. 1st postback happens the first time you click the button, 2nd postback happens the 2nd time you click the button
    In this scenario, LoadViewState will only be called during the 2nd postback

  5. Very good explanation. Thanks for the post.

Trackbacks/Pingbacks

  1. Exploring Sharepoint Connected Web Parts with IWebPartRow « More Soma Please… - October 31, 2008

    [...] far as web part life cycle events go, things follow the normal web part life cycle model (see this post), however, the call to SetConnectionInterface() follows OnLoad(). This call connects the 2 web [...]

  2. Sharepoint Webpart Lifecycle Events Updated « More Soma Please… - January 14, 2009

    [...] by platinumdogs on January 14, 2009 I’ve updated my post on Sharepoint WebPart Lifecycle events, since the previous version wasn’t that [...]

  3. Czech Information Worker Community » Sharepoint WebPart LifeCycle and Event flow - April 14, 2009

    [...] source: http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/ [...]

  4. Localzuk » Blog Archive » Sharepoint WebPart tips - April 30, 2010

    [...] This site gives a very useful list of the ‘lifecycle’ of a webpart. It lists the order in which different methods are called. This allows a new developer to avoid making a mistake such as trying to replace controls on the page dynamically.¬†http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/ [...]

  5. 2010 in review « More Soma Please… - January 2, 2011

    [...] 10 Posts Sharepoint (MOSS): Creating Document Library Items / Uploading Files to a Document LibrarySharepoint WebPart Lifecycle Events.NET: Customizing XML Object SerializationInstalling the Sysprep Tool for Windows 2003 [...]

  6. When to Fetch Data passed into a Consumer Web Part from a Provider Web Part | Site Assets - November 4, 2011

    [...] http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/ Share this:TwitterEmailMoreFacebookLike this:LikeBe the first to like this post. [...]

  7. SPGridView: Paging does not work | Bram's SharePoint Adventure - February 14, 2012

    [...] the correct page of the DataSet. More info on the lifecycle of a SharePoint webpart can be found here. Share this:Like this:LikeBe the first to like this [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 571 other followers

%d bloggers like this: