2

Can anyone tell me that what is the life cycle of web part in SharePoint ?

3
  • Can you please add some more details?
    – Asad Refai
    Sep 27, 2016 at 9:26
  • Which type of details you want from me about this question ? I am asking about the simple custom web part life cycle. Sep 27, 2016 at 9:32
  • An elaborated question is always better, and you will get specific answers.
    – Asad Refai
    Sep 27, 2016 at 9:40

2 Answers 2

4

Following Life cycles are available in SharePoint 2010.

There are four predefined routines that take place in a WebPart Life Cycle,

OnInit
CreateChildControls
OnPrerender
Render 

OnInit: This method handles initialization of the control.

Example

protected override void OnInit(EventArgs e)  
{  
   try  
   {  
      _divPanel = new Panel();  
      _ddlCountry = new DropDownList();  

      _ddlTown = new DropDownList();  
   }
}

OnLoad: This event handles the Load event and also used for initialize the control. But it is not intended for loading data or other processing functionality.

CreateChildControls: This creates any child controls.

In most cases, we have to initialize the control's default value (as Text, Checked, Items, and so on) and activity that is possible to call just at first WebPart load, checking PostBack:

Example

    protected override void CreateChildControls()  
    {  
       try  
       {  
          base.CreateChildControls();  
          _divPanel.ID = "_divPanel";  
          _divPanel.BorderStyle = BorderStyle.Ridge;  
          _divPanel.Width = 250;  
          _divPanel.Height = 300;  
          _divPanel.BackColor = System.Drawing.Color.Gainsboro;  
          Controls.Add(_divPanel);  
       }
    }

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.This is the routine where the control's value kept by ViewState (for example Item value set up by the user to DropDownList nation), is available.

For this reason, it's normal to begin business procedure from here, and in our case we're going to load values for the second dropdown list (Town DropDownList) with the proper data.

Page.PreRenderComplete: The page fires the PreRenderComplete event after all controls have completed their OnPreRender methods.

Render: This method is used to render everything.That's the routine for HTML rendering WebPart.

RenderContents: Renders the contents of the control only, inside of the outer tags and style properties.

OnUnload: Performs the final clean up.

0
1

In case you are referring to the new Sharepoint Framework webpart lifecycle, I woulfd suggest you checking both of these links:

2
  • and remove /es-es in the URL to get the pages in your locale.
    – Christophe
    Mar 17, 2021 at 19:26
  • 1
    thanks, just updated it
    – Alberto S.
    Mar 18, 2021 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.