[英]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. 我已经创建了一个Sharepoint WebPart并成功部署了它并将其添加到页面中。
It should sport a Label, a TextBox, and a button, but it does not. 它应该带有一个Label,一个TextBox和一个按钮,但是没有。 It is "naked", as you can see 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;
}
}
}
? ?
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. ...重建,部署此WebPart的另一个实例,并将其添加到测试页中,但是,可惜的是,在Mudville还是不高兴。
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:
我尝试了三种不同的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
...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).
因此,我假设当我仅使用“ WebPart”时,它就解析为“ System.Web.UI.WebControls.WebParts.WebPart”(IOW,第二个和第三个实验是相同的)。 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...
对于我来说,这两个不同的祖先(System.Web.UI.WebControls.WebParts.WebPart和Microsoft.SharePoint.WebPartPages.WebPart)似乎都在一定程度上起作用了,这让我有些奇怪。
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. ...但也无济于事-我可以将WebPart添加到页面上,直到母牛回家,但我添加的控件仍然不显示。
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 如果删除渲染的替代并将文字添加到CreateChildControls
lt = new Literal();
lt.Text = MakeHtml();
Controls.Add(lt);
OR try adding this to your RenderContents Method. 或尝试将其添加到您的RenderContents方法。
foreach(Control control in Controls)
{
control.RenderControl(writer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.