Understanding the Sharepoint Webpart Lifecycle

The aim of this write up is to highlight the ViewState WebPart life Cycle and understand Text property i.e. when the value of a control which has got ViewState set up to becomes available. Basically, this only happens when the users select a nation that change the first dropdown list item that often begins the Postback event. It is during the Postback event that there will be availability of the value of the new item. Let’s now get to know what is normally involved in the webpart lifecycle.

SharePoint Webpart Lifecycle

Below are the various steps for the SharePoint Webpart Lifecycle:

Step#1: OnInit

This technique is usually meant to handle initialization of the control.

Step#2: OnLoad

The event is supposed to handle the load event. Generally, OnLoad can also be used to initialize the control. However, it cannot be used to load data or process the other functionality.

Step#3: CreateChildControls

When it is about web part life cycle, this process is usually considered as the most popular. CreateChild can create any child controls easily. Therefore, if you intend to add any child controls to display, you’ve got to write in this technique.

In this routine, control methods and properties previously declared are often defined. However in most situations, you will be required to initialize the default value of the controls such as Items, Text and Checked as well as activity which is possible to call at the first WebPart load that is checking PostBack.

The controls that have been specified are often created and then added to the control collections. During this stage, you can also make use of EnsureChildControls since it can check as well as tell if the CreateChildControls technique has been called. If not yet called, EnsureChildControls calls it.

Step#4: OnPreRender

OnPreRender is meant to set up any business logic related work. In this routine it controls the value kept by the viewstate. Here you can change any web part property before control output is drawn. Basically, this is often the routine whereby the controls’ value is preserved so that it can be used for View State purposes.

Step#5: Render

This technique is meant to render everything. This is the stage where HTML Output is normally generated. In this stage of the SharePoint Webpart Lifecycle, event creates HTML which appears on the Web Part’s body. The CreateChildControls and RenderContents technique are again used in this stage. However, in this stage the CreateChildControls is meant to add controls to WebPart. The RenderContents technique is used to inform the page framework how it can render the control into the HTML. This helps the HTML to display itself on a page.

Step#6: OnUnload

OnUnload is meant to perform the final clean up. It is executed each time the web part happens to be unloaded.

Step#7: Dispose

This is the last stage that is involved in the webpart lifecycle. Typically, in this stage the memory is freed.

If you follow the steps that are involved with Sharepoint webpart lifecycle, you will certainly experience their effectiveness.

Marissa Hart is the Lead Author & Editor ShareMe. ShareMe is a blog focused on SharePoint Online. SharePoint Online delivers the powerful features of SharePoint without the associated overhead of managing the infrastructure.