自定義屬性使用額外的選項和設置拓展你的Web part部件。本文主要講解如何使用Visual Studio創建簡單的自定義Web Part 部件屬性。
1. 打開Visual Studio,點擊文件--新建項目--空白SharePoint項目CustomWPProperties。部署為場解決方案。
2. 右擊項目添加新項Web Part部件WPPropertyExample,點擊添加。
3. 右擊WPPropertyExample.cs,點擊查看代碼。
4. 添加代碼,最後是這樣的:
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 System.Xml.Serialization;
namespace CustomWPProperties.WPPropertyExample
{
public class WPPropertyExample : WebPart
{
Label lblTitle = new Label();
public enum SharePointLists
{
Stats,
Products,
Customers,
}
protected SharePointLists listOfList;
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("Available Lists"),
WebDescription("Available Lists in SharePoint Site.")]
public SharePointLists MySticks
{
get { return listOfList; }
set { listOfList = value; }
}
protected override void CreateChildControls()
{
lblTitle.Text = "Web Part with Property";
this.Controls.Add(lblTitle);
}
}
}
5. 點擊生成--部署解決方案。
6. 在SharePoint站點,點擊網站操作--編輯頁面--添加Web部件,在Custom類中選中WPPropertyExample,點擊添加。
7. 編輯這個Web部件,在屬性窗格可以看到:

本欄目