为什么我的 WebPart 上的控件不显示?
Why are the controls on my WebPart not displaying?
我已经创建了一个 Sharepoint WebPart 并成功部署它并将其添加到页面。
它应该有一个标签、一个文本框和一个按钮,但它没有。它是 "naked",如您所见:
为什么控件不显示?这是最简单的代码:
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 添加到页面,直到奶牛回家,但我添加的控件仍然没有显示。
乍一看,您似乎无意中覆盖了功能。添加页面级文字控件。Literal lt;
如果您删除对渲染的覆盖并向 CreateChildControls 添加文字
lt = new Literal();
lt.Text = MakeHtml();
Controls.Add(lt);
或者尝试将其添加到您的 RenderContents 方法中。
foreach(Control control in Controls)
{
control.RenderControl(writer);
}
我已经创建了一个 Sharepoint WebPart 并成功部署它并将其添加到页面。
它应该有一个标签、一个文本框和一个按钮,但它没有。它是 "naked",如您所见:
为什么控件不显示?这是最简单的代码:
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 添加到页面,直到奶牛回家,但我添加的控件仍然没有显示。
乍一看,您似乎无意中覆盖了功能。添加页面级文字控件。Literal lt;
如果您删除对渲染的覆盖并向 CreateChildControls 添加文字
lt = new Literal();
lt.Text = MakeHtml();
Controls.Add(lt);
或者尝试将其添加到您的 RenderContents 方法中。
foreach(Control control in Controls)
{
control.RenderControl(writer);
}