Understanding Web Part life cycle

Web Part Life Cycle starts with

OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part.

LoadViewState – The view state of the web part is populated over here.

CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. 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.

OnLoad

User Generated Event – for e.g. button click on the web part.

OnPreRenderHere we can change any of the web part properties before the control output is

drawn.

RenderContents – Html Output is generated.

SaveViewState - View state of the web part is serialized and saved.

Dispose

UnLoad.

Bye…

8 thoughts on “Understanding Web Part life cycle

  1. This Post is Wrong!

    SaveViewState before RenderContents, and …

    UnLoad before Dispose … (how can you unload something after the object is disposed).

    This will also vary depending if it is a postback as well.

  2. This is not correct.

    RenderContents happens after SaveViewState, ..and

    Unload happens before Dispose. How can it unload if the object has been marked for garbage collection?

    There are a lot more events that happen, and in differing order depending on wherther it is a PaheLoad or after a postpback.

  3. thanx alot i needed this, but i have a question and hopefully I find an answer here for it

    on a button click event, typical add item to a list situation, why the page doesnt populate the new resulted addition, why do i have to refresh the browser explicitly

    thanx again

  4. In my case the LoadViewState is not getting called on postback. Only SaveViewState method is called.
    What is that I am missing here?
    I am sure the page is getting posted back though.
    Thank you in advance.

  5. Pingback: Links (2/15/2009) « Steve Pietrek - Everything SharePoint

Share your thoughts

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