Tag Archives: Events

Web Part Life Cycle Events

There are various web part events in the life-cycle. The following would shed some light to these events and help us to choose these events correctly based on the needs

OnPreRender:

It is called logically called before Render event of the ASP.NET page. This is the last opportunity for us to affect the rendering behavior of the web part page. Things like changes to the viewstate properties can be done logically in the OnPreRender event.   Moreover the OnPreRender  event is called logically after the events EnsureChildControls and CreateChildControls

CreateChildControls:

If we need to render the ASP.NET web server controls (ike button, text box etc) in the web part, this event needs to be overridden. Logically we can add web controls and wire the events to it

Render:

This event is used logically to control the rendering behavior of the web part. There is already a default implementation of Render method. We need not always override Render method for certain cases like CreateChildControls method having the logic to add controls. Render method can also be used to render html stream (directly) and ASP.NET web server controls

Render Control:

This method can be called inside the overridden Render method to render the ASP.NET web server controls like button, textbox etc…

Render Contents:

RenderContents gives power to render the html streams directly inside the overridden Render method. The RenderContents should be preceded by RenderBeginTag and the RenderContents should be followed RenderEndTag

Source: http://msmvps.com/blogs/sundar_narasiman/archive/2008/02/14/web-part-life-cycle-events.aspx