Welcome

首页 / 软件开发 / .NET编程技术 / 如何使用VS创建简单的自定义Web Part 部件属性

如何使用VS创建简单的自定义Web Part 部件属性2015-01-01自定义属性使用额外的选项和设置拓展你的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部件,在属性窗格可以看到: