我创建了一个 Sharepoint WebPart 并成功部署它并将其添加到页面中。
它应该带有一个标签、一个文本框和一个按钮,但它没有。它是“裸体的”,如您在此处看到的:
为什么控件不显示?这是相当少的代码:
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
...而且它们似乎都工作相同。所以我假设当我简单地使用“WebPart”时,它会解析为“System.Web.UI.WebControls.WebParts.WebPart”(IOW,第二个和第三个实验是相同的)。对我来说有点奇怪的是,这两个不同的祖先(System.Web.UI.WebControls.WebParts.WebPart 和 Microsoft.SharePoint.WebPartPages.WebPart)似乎都在有限的范围内工作,无论如何......
更新 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
}
...但也无济于事 - 我可以将 WebPart 添加到页面,直到奶牛回家,但我添加的控件仍然不显示。