本篇文章介绍的是在sharepoint 2010中用VS 2010开发自定义webpart。
自定义开发的webpart分为两种:带参数的和不带参数的;带参数的分为需要绑定外部数据的和不需要绑定外部数据的。
不带参数的webpart:
这种webpart开发最简单,只需要将webpart页面的后台逻辑处理完就可以,不需要外来的参数。
不需要绑定外部数据的带参数的webpart:
这种webpart开发,需要在继承system.web.ui.webcontrols.webparts.webpart的类中增加参数,即如下形式的代码:
[Personalizable(),
WebDisplayNameAttribute("FullUrl"),
WebBrowsable]
public string FullUrl
{
get;
set;
}
也可增加emun类型的下拉列表参数(但是其中的参数是在代码中写死的)。
需要绑定外部数据的带参数的webpart:
这种webpart的开发相对比较麻烦,除了需要在继承system.web.ui.webcontrols.webparts.webpart的类中增加参数之外,还需要在继承system.web.ui.webcontrols.webparts.Editorpart的类中绑定外来数据并传给继承system.web.ui.webcontrols.webparts.webpart的类中的参数。
在继承system.web.ui.webcontrols.webparts.Editorpart的类中,需要重载方法:
1. protected override void CreateChildControls():该方法是用来构建编辑webpart时选择参数的页面;
2.public override void SyncChanges():该方法是用来绑定外部数据到webpart编辑页面的控件中的;
3.public override bool ApplyChanges():该方法是用来将编辑页面中控件的值回传给继承system.web.ui.webcontrols.webparts.webpart的类中的参数。
例子如下:
[ToolboxItemAttribute(false)]
public class Statistics_Monthly : WebPart
{
[Personalizable(),
WebDisplayNameAttribute("工作量统计类别")]
public string BisicCategory
{
get;
set;
}
[Personalizable(),
WebDisplayNameAttribute("工作量统计时间")]
public string StatisticsTime
{
get;
set;
}
// 当更改可视 Web 部件项目项时,Visual Studio 可能会自动更新此路径。
private const string _ascxPath = @"~/_CONTROLTEMPLATES/StatisticsWebPart_Monthly/Statistics_Monthly/Statistics_MonthlyUserControl.ascx";
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
if (control != null)
{
((Statistics_MonthlyUserControl)control).webPart = this;
}
Controls.Add(control);
}
public override EditorPartCollection CreateEditorParts()
{
EditorPartCollection baseParts = base.CreateEditorParts();
List<EditorPart> editorParts = new List<EditorPart>(1);
EditorPart part = new Statistics_Monthly_Edit();
part.ID = this.ID + "_Statistics_Monthly_Editor";
part.Title = "Statistics_Monthly变量选择";
editorParts.Add(part);
return new EditorPartCollection(baseParts, editorParts);
}
}
public class Statistics_Monthly_Edit : EditorPart
{
protected Label label_BasicCategory;
protected DropDownList ddl_BasicCategory;
protected Label label_Time;
protected DropDownList ddl_Year;
protected Label label_Year;
protected DropDownList ddl_Month;
protected Label label_Month;
protected override void CreateChildControls()
{
base.CreateChildControls();
this.label_BasicCategory = new Label();
this.label_BasicCategory.Text = "工作量统计类型:";
this.ddl_BasicCategory = new DropDownList();
this.ddl_BasicCategory.AutoPostBack = true;
this.ddl_BasicCategory.SelectedIndexChanged += new EventHandler(ddl_BasicCategory_SelectedIndexChanged);
this.label_Time = new Label();
this.label_Time.Text = "请选择显示时间:";
this.ddl_Year = new DropDownList();
this.ddl_Year.AutoPostBack = true;
this.ddl_Year.SelectedIndexChanged += new EventHandler(ddl_Year_SelectedIndexChanged);
this.label_Year = new Label();
this.label_Year.Text = "年";
this.ddl_Month = new DropDownList();
this.ddl_Month.AutoPostBack = true;
this.ddl_Month.SelectedIndexChanged += new EventHandler(ddl_Month_SelectedIndexChanged);
this.label_Month = new Label();
this.label_Month.Text = "月";
Table tb = new Table();
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(this.label_BasicCategory);
cell2.Controls.Add(this.ddl_BasicCategory);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tb.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Controls.Add(this.label_Time);
cell2.Controls.Add(this.ddl_Year);
cell2.Controls.Add(this.label_Year);
cell2.Controls.Add(this.ddl_Month);
cell2.Controls.Add(this.label_Month);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tb.Rows.Add(row);
this.Controls.Add(tb);
}
protected void ddl_BasicCategory_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ddl_Month_SelectedIndexChanged(object sender, EventArgs e)
{
}
public override bool ApplyChanges()
{
this.EnsureChildControls();
Statistics_Monthly webpart = this.WebPartToEdit as Statistics_Monthly;
if (webpart == null)
{
return false;
}
else
{
if (this.ddl_BasicCategory.SelectedItem != null)
{
webpart.BisicCategory = this.ddl_BasicCategory.SelectedValue;
}
else
{
webpart.BisicCategory = string.Empty;
}
if ((this.ddl_Year.SelectedItem != null) && (this.ddl_Month.SelectedItem != null))
{
webpart.StatisticsTime = this.ddl_Year.SelectedValue + "/" + this.ddl_Month.SelectedValue;
}
else
{
webpart.StatisticsTime = string.Empty;
}
return true;
}
}
public override void SyncChanges()
{
EnsureChildControls();
Statistics_Monthly webpart = this.WebPartToEdit as Statistics_Monthly;
if (webpart != null)
{
InitBasicCategory(webpart);
InitStatisticsTime(webpart);
}
}
private void InitBasicCategory(Statistics_Monthly webpart)
{
从外部获取值并绑定到this.ddl_BasicCategory下拉列表中。
}
private void InitStatisticsTime(Statistics_Monthly webpart)
{
从外部获取值并绑定到this.ddl_Year和this.ddl_Month的dropdownlist中的值。
}
}