我正在SharePoint 2010中创建Web部件。我知道我们可以使用属性上的特定属性在Web部件设置中添加其他输入字段(例如CheckBox,TextBox等)。
现在,有没有选项可以添加指向网页的链接,比如跟随模型?
答案 0 :(得分:3)
要添加复选框(布尔值)或文本框(字符串)以外的任何内容,您需要做更多工作并创建自定义ToolPart对象。 this question有一个很好的基础,下面是一些示例代码,展示了如何执行此操作的基础知识:
using System;
using System.ComponentModel;
using System.Web;
namespace SharePointProject1.SimpleWebPart
{
[ToolboxItemAttribute(false)]
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
protected override void CreateChildControls()
{
//Do the actual work of the web part here
}
public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()
{
//First add base tool parts and then our simple one with a hyperlink
Microsoft.SharePoint.WebPartPages.ToolPart[] toolPartArray = new Microsoft.SharePoint.WebPartPages.ToolPart[3];
toolPartArray[0] = new Microsoft.SharePoint.WebPartPages.CustomPropertyToolPart();
toolPartArray[1] = new Microsoft.SharePoint.WebPartPages.WebPartToolPart();
toolPartArray[2] = new mySimpleToolPart();
return toolPartArray;
}
}
//Implements a custom ToolPart that simply displays a link
public class mySimpleToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
public mySimpleToolPart()
{
//You could pass this as a parameter to the class
this.Title = "This is the title";
}
protected override void CreateChildControls()
{
System.Web.UI.WebControls.HyperLink simpleLink = new System.Web.UI.WebControls.HyperLink();
simpleLink.Text = "Click on this Link";
simpleLink.NavigateUrl = "http://www.google.com";
Controls.Add(simpleLink);
//Add some white space
Controls.Add(new System.Web.UI.HtmlControls.HtmlGenericControl("p"));
}
}
}