为什么我的WebPart上的控件没有显示?

时间:2015-03-23 18:57:31

标签: sharepoint sharepoint-2010 web-parts webpartpage createchildcontrols

我创建了一个Sharepoint WebPart并成功部署了它并将其添加到页面中。

它应该运行Label,TextBox和按钮,但事实并非如此。它是赤裸裸的",你可以在这里看到:

enter image description here

为什么控件没有显示?这是相当简单的代码:

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

    }
}

更新

我想也许我必须明确调用,所以我在构造函数中添加了对它的调用:

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

...重建,部署并将此WebPart的另一个实例添加到测试页面,但是,唉,在Mudville仍然没有喜悦。

更新2

我正在尝试任何想到的内容,看看各种调整是否会导致控件显示。我尝试了三种不同风格的WebPart祖先:

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

......他们似乎都是一样的。所以我假设我只是使用&#34; WebPart&#34;它正在解析为&#34; System.Web.UI.WebControls.WebParts.WebPart&#34; (IOW,第二次和第三次实验是相同的)。这对我来说有点奇怪,这两个不同的祖先(System.Web.UI.WebControls.WebParts.WebPart和Microsoft.SharePoint.WebPartPages.WebPart)似乎都在有限的范围内工作......无论如何...... / p>

更新3

我也试过添加它:

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
}

...但也无济于事 - 我可以将网页添加到页面中,直到奶牛回家,但我添加的控件仍然没有显示出来。

1 个答案:

答案 0 :(得分:1)

乍看之下,看起来你可能会无意中压倒功能。添加页面级文字控件。Literal lt;

如果删除了render的覆盖并将文字添加到CreateChildControls

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

或尝试将此添加到RenderContents方法。

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