您的位置:首页 > 运维架构

sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties

2013-06-03 16:01 633 查看
webpart 是在sharepoint开发过程中,最常用的一种方式。扩展webpart自定义属性边栏字段,可以做到动态给webpart配置参数。如下图所示,在杂项里面,我们看到有三个属性,company,url, city,这个就是我们自定义的3个webpart属性。



最终我们要实现的效果,就是动态给webpart传递这些参数值。如下图所示:



1。创建一个sharepoint project 3,5项目,WebpartBarTest,并且添加一个可视化部件WebpartBarProperties,

如下图所示 :



2。在页面上添加几个服务器控件,label.

<table border="1px">

    <tr>

        <td>公司名称:

        </td>

        <td><asp:Label ID="lblCompany" runat="server" Text="" Font-Bold="true"></asp:Label>

        </td>

    </tr>

    <tr>

        <td>所在地:

        </td>

        <td><asp:Label ID="lblUrl" runat="server" Text="" Font-Bold="true"></asp:Label>

        </td>

    </tr>

    <tr>

        <td>公司网址:

        </td>

        <td><asp:Label ID="lblCity" runat="server" Text="" Font-Bold="true"></asp:Label>

        </td>

    </tr>

</table>

3.。后台实现代码

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.ComponentModel;

namespace WebpartBarTest.WebpartBarProperties

{

    public partial class WebpartBarPropertiesUserControl : UserControl

    {

        /// <summary>

        /// 自定义的webpart属性

        /// </summary>

        public WebpartBarProperties WebPart { get; set; } 

        protected void Page_Load(object sender, EventArgs e)

        {

            this.lblCompany.Text =WebPart.Company;

            this.lblCity.Text =WebPart.City;

            this.lblUrl.Text =WebPart.Url;

        }

       

    }

}

 

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;

namespace WebpartBarTest.WebpartBarProperties

{

    [ToolboxItemAttribute(false)]

    public class WebpartBarProperties : WebPart

    {

        // 当更改可视 Web 部件项目项时,Visual Studio 可能会自动更新此路径。

        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WebpartBarTest/WebpartBarProperties/WebpartBarPropertiesUserControl.ascx";

       

        protected override void CreateChildControls()

        {

            WebpartBarPropertiesUserControl control = Page.LoadControl(_ascxPath) as WebpartBarPropertiesUserControl;

            //添加自定义属性

          
ab12
  if (control != null) 

            { 

                control.WebPart = this;

            } 

            Controls.Add(control);

        }

        [Personalizable(), WebBrowsable] 

        public String Company { get; set; }

        [Personalizable(), WebBrowsable]

        public String Url { get; set; } 

        [Personalizable(), WebBrowsable]

        public String City { get; set; } 

    }

}

4。部署到sharepoint站点上,将这个webpart添加到页面上,并且给我们自定义的3个属性赋值。



点击确定,这样就完成了我们对webpart自定义属性的扩展。这种方式在sharepoint的开发非常常用。



 程序下载地址:http://download.csdn.net/download/cxx2325938/4898105 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息