Wednesday, July 14, 2010

SharePoint WebPart life cycle

Life Cycle of sharepoint Webparts:


1. Protected override void OnInit(EventArgs e)


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


2. Protected override void OnLoad(EventArgs e)


OnLoad

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


3. Protected override void CreateChildControls()


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.


4. Protected override void LoadViewState(object savedState) //Only at Postback


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


5. protected override void OnPreRender(EventArgs e)


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

drawn.


6. protected override void Render(System.Web.UI.HtmlTextWriter writer)


RenderContents – Html Output is generated.


7. protected override void OnUnload(EventArgs e)
8. public override void Dispose()