locked
when to override CreateChildControl, PreRender, Render or RenderContents

    Question

  •  

    can someone show me when to override CreateChildControl, PreRender, Render or RenderContents?

     

    It seems to me I only need to override CreateChildControl to create and format controls to the web part. Am I right?

    But I've seen someone override both CreateChildControl and Render and doing something within them.

     

    Also, should I override PreRender, instead of the Render, or RenderContents, if I don't use the CreateChildControl?

     

    It just confuse me.

     

    Thanks in advance.

     

     

    Friday, February 08, 2008 9:46 PM

Answers

  • 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 be overridden to render both the html stuff 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

     

    Thursday, February 14, 2008 11:53 AM

All replies

  • Great question.  I think there is a difference between the sharepoint webpart and an asp.net webpart in regards to your question, I think around the render/prerender area.  Could someone please also answer this? 

     

    Friday, February 08, 2008 10:11 PM
  • Hi

    Sharepoint webpart uses RenderWebpart() method instead of Render(). Render() is used in case if you are working with ASP.NET webpart.

    Use CreateChildControls() for
    * Instantiating your controls
    * Wire events to controls
    * Adding controls to controls collection

    Use Render() for
    * To add logic to determine weather controls should be displayed or not
    * Render the controls using control.RenderControl()

    HTH

    Madhur

    Saturday, February 09, 2008 4:48 PM
  • thanks for your respond.

     

    you mention that

     

    Use Render() for
    * Render the controls using control.RenderControl()

    do you mean that I have to call the RenderControl() on each of the controls that I want it to display and don't need to call the method if the controls are not displaying?

     

     

    Tuesday, February 12, 2008 4:00 PM
  • Hi

     

    Exactly. However as an alternative you can add the controls to Control Collection and call the Render method for the base class. In that case base class automatically calls the RenderControl() method for you.

     

    Madhur

     

    Wednesday, February 13, 2008 3:35 AM
  • ok. thanks.

     

    now, how about PreRender, Render or RenderContents, which one should i override? Someone prefer the PreRender and leave Render() for WSS.

     

    Wednesday, February 13, 2008 10:28 PM
  • 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 be overridden to render both the html stuff 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

     

    Thursday, February 14, 2008 11:53 AM
  • I've noticed that if I have the following code in my WebPart:
    protected override void Render(HtmlTextWriter writer)
    {
        //TODO: add custom rendering code here.
        writer.Write("Output HTML");
    }
    
    protected override void CreateChildControls()
    {
       try
       {
           this.Controls.Clear();
    
           wpUserControl gridUserControl = (wpUserControl)this.Page.LoadControl("/_CONTROLTEMPLATES/wpUserControl.ascx");
    
           this.Controls.Add(gridUserControl);
       }
       catch (Exception ex)
       {
           // Put the error messsage locally on the page
           string errMessage = ex.GetType().ToString() + ": " + ex.Message;
           this.Controls.Add(new LiteralControl(errMessage));
       }
    }
    That "Output HTML" is written to the page and I assume erases all that I have done in the CreateChildControls method.

    J
    Wednesday, February 24, 2010 7:21 PM
  • That is correct. Any controls you add to the Controls property (collection) will only get rendered if the RenderChildren() method is called. RenderChildren is typically called by base.Render() method in the WebControl class which is usually called by the RenderContents() method which is called by the Render() method.
    Wednesday, May 05, 2010 4:53 PM
  •  

    Don't use Render(), only use RenderContents() or use the more OO  approach and implement CreatechildControls().

    Andrew clearly explained why not to use Render() method in this article.

    http://www.andrewconnell.com/blog/archive/2008/02/18/Understanding-how-Web-Parts-are-rendered-why-to-never-use.aspx

    Monday, September 06, 2010 9:34 AM
  • !!! DO NOT OVERRIDE CreateChildControls() !!!

    You will run into a world of hurt, especially when it comes to subscribing to child control events! Instead, create a private method called, "CreateCustomChildControls()". Build your control tree there. Simply override OnInit() and call CreateCustomChildControls() from there.

    You can buy me a beer later.

    Thursday, November 18, 2010 5:07 PM