sharepointsharepoint-2010renderingcreatechildcontrols

Does overriding Sharepoint's Render method accomplish the same thing as overriding CreateChildControls?


Based on what I've read (I'm new to SP), ISTM that CreateChildControls() is the preferred method for dynamically adding controls to a WebPart, a la:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    lbl = new Label();
    lbl.Text = "Look at this";
    this.Controls.Add(lbl);

    tb = new TextBox();
    this.Controls.Add(tb);

    btnSave = new Button();
    btnSave.Width = new Unit(50, UnitType.Pixel);
    btnSave.Text = "Click me if you dare";
    btnSave.Click += new EventHandler(btnSave_Click);
    this.Controls.Add(btnSave);
}

However, I see in the legacy code here that CreateChildControls() is not used; instead, I see code like this, overriding Render():

protected override void Render(HtmlTextWriter output)
{
    try
    {
        List<Announcement> announcements = getAnnouncements(AnnouncementsList);
        if (announcements.Count > 0)
            output.Write(makeHTML(announcements));
    }
    catch (Exception e)
    {
        output.Write("Exception: " + e.Message);
    }
}

...which then calls a custom method to get HTML to render:

protected string makeHTML(List<Announcement> announcementList)
{
    string r = "<div class='platypus-announcements-panel'>";
    r += "<h2 class='platypus-white-panel-title'>" + Header + "</h2>";
    r += "<dl>";
    foreach (Announcement curr in announcementList)
    {
        r += "<dt>" + curr.date.ToString("MMM") + "<strong>" + curr.date.Day + "</strong>" + curr.date.ToString("yyyy") + "</dt>";
        r += "<dd>" + curr.content + "</dd>";
    }
    r += "</dl>";
    r += "</div>";
    return r;
}

Is directly adding HTML in this way preferable to dynamically creating controls, as shown above in CreateChildControls()?


Solution

  • There's not a preferred one way or another, they are for different purposes.

    For an overview of the ASP.NET control lifecycle, see https://msdn.microsoft.com/en-us/library/aa719775(v=vs.71).aspx. It says it is for .NET 1.1, but is still relevant to your question.

    The CreateChildControls method enables you to add additional controls to the control tree, such as in your example that adds a textbox and a button. This defers rendering and postback handling of the control to the controls in the control tree. CreateChildControls can be called anywhere in the control lifecycle.

    The Render method happens at a specific part of the control lifecycle and is only responsible for rendering HTML.

    Note that it is possible to create a control that uses both CreateChildControls() and Render().

    Your question mentions SharePoint, which sounds like you are asking about server-side controls that are installed on SharePoint. I provide an overview of user controls and server controls with an example of using CreateChildControls() in the post http://blogs.msdn.com/b/kaevans/archive/2011/04/28/user-controls-and-server-controls-in-sharepoint.aspx. However, this approach is not recommended for SharePoint 2013, it is recommended that you move development to the app model... in which case you are not limited to SharePoint's use of the ASP.NET Forms model, you can use other models such as ASP.NET MVC or even other platforms such as PHP, Node.js, Java, etc. See http://dev.office.com for more information on the app model.