Tag Archives: Web Part

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

Create child controls the right way in webparts and composite controls

Some of the things to watch out for when overriding CreateChildControls:

  1. Instantiate a control as a first step, add a control to the Controls collection as a last one. The moment you add the control into the Controls collection, it will play what is called a “catch-up” where it flows through the lifecycle phases until it reaches the current phase of the page. Adding control to the Controls collection has also important implications when it comes to what is being considered an initial data (data you would normally put in your xml markup on the page or the user control). Any property values you assign before adding the control to the collection are not persisted in view state.
  2. Call base.CreateChildControl method, since you are overwriting the base functionality. This is not a mandatory step, but more often than not, you want to take advantage of the functionality that the child controls of the base class provide.
  3. Do not rely on IsPostBack when loading control with data in web parts. This is a documented issue, where by adding the web part to the page the postback occurs. From that point on, every subsequent request will be considered a postback.
  4. Take advantage of events in the child controls. This ensures proper flow of execution handled by the controls themselves. For instance, use Load event of the GridView control to load the data.
  5. Do not call CreateChildControls explicitly, rather rely on the EnsureChildControls() method to make sure controls have been properly loaded. This is especially true when exposing properties that delegate to the properties of the child control. You don’t have to worry about executing the same logic many times – EnsureChildControls checks the ChildControlsLoaded boolean flag, which is set by the CreateChildControls method, first time that method is called. If it hasn’t been called up to controls PreRender event, the PreRender will call CreateChildControls so that the controls are properly instantiated prior to the Render event.

Source: http://pfefferkorn.net/CS07/blogs/tomek/archive/2007/04/28/test-blog.aspx