sharepointsharepoint-2010web-partswebpartpagecreatechildcontrols

Why are the controls on my WebPart not displaying?


I have created a Sharepoint WebPart and successfully deployed it and added it to a page.

It should sport a Label, a TextBox, and a button, but it does not. It is "naked", as you can see here:

enter image description here

Why would it be that the controls are not displaying? Here is the rather minimal code:

namespace WebFormPDFGen.WebFormPDFGenWebPart
{
    //[ToolboxItemAttribute(false)]
    public class WebFormPDFGenWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Label lbl = null;
        TextBox tb = null;
        Button btnSave = null;

        [Personalizable(PersonalizationScope.Shared)]
        public string Header { get; set; }

        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);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            lbl.Text += lbl.Text + "now!";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            EnsureChildControls();

            String user = SPContext.Current.Web.Users[0].ToString();
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.Write(makeHTML());
        }

        protected string makeHTML()
        {
            string r = "<div class='finaff-announcements-panel'>";
            r += "<h2 class='finaff-white-panel-title'>" + Header + "</h2>";
            r += "<dl>";
            r += "</dl>";
            r += "</div>";
            return r;
        }

    }
}

?

UPDATE

I thought maybe I had to explicitly call, so I added a call to it in the constructor:

public WebFormPDFGenWebPart() 
{
    CreateChildControls();
    CssRegistration css = new CssRegistration();
    css.After = "corev4.css";
    css.Name = "/Style Library/dplat_style_webparts.css";
    this.Controls.Add(css);
}

...rebuilt, deployed, and added another instance of this WebPart to a test page but, alas, still no joy in Mudville.

UPDATE 2

I'm experimenting with whatever comes to mind to see if various tweaks can cause the controls to display. I tried three different flavors of WebPart ancestors:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Word.Server.Conversions;
using System.Collections.Generic;

namespace WebFormPDFGen.WebFormPDFGenWebPart
{
    //[ToolboxItemAttribute(false)]
    public class WebFormPDFGenWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    //public class WebFormPDFGenWebPart : WebPart
    //public class WebFormPDFGenWebPart : System.Web.UI.WebControls.WebParts.WebPart

...and they all seem to work the same. So I assume when I simply use "WebPart" it is resolving to "System.Web.UI.WebControls.WebParts.WebPart" (IOW, the second and third experiments are the same). It's a little odd to me that these two different ancestors (System.Web.UI.WebControls.WebParts.WebPart and Microsoft.SharePoint.WebPartPages.WebPart) both seem to work, to a limited extent, anyway...

UPDATE 3

I tried adding this, too:

protected override void RenderContents(HtmlTextWriter writer)
{
    writer.RenderBeginTag(HtmlTextWriterTag.Table);
    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.Write("lbl Test: ");
    writer.RenderEndTag();
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    lbl.RenderControl(writer);
    writer.RenderEndTag();
    writer.RenderEndTag();

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.Write("TextBox: ");
    writer.RenderEndTag();
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    tb.RenderControl(writer);
    writer.RenderEndTag();
    writer.RenderEndTag();

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.Write("save button: ");
    writer.RenderEndTag();
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    btnSave.RenderControl(writer);
    writer.RenderEndTag();
    writer.RenderEndTag();

    writer.RenderEndTag(); // table
}

...but also to no avail - I can add the WebPart to a page until the cows come home, but the controls I've added still don't show up.


Solution

  • Just at a glance, it looks like you may be overriding functionality unintentionally. Add the page level literal control.Literal lt;

    If you remove your override of render and add a literal to CreateChildControls

    lt = new Literal();
    lt.Text = MakeHtml();
    Controls.Add(lt);
    

    OR try adding this to your RenderContents Method.

    foreach(Control control in Controls)
    {
        control.RenderControl(writer);
    }