您的位置:首页 > 其它

sharepoint 2010 custom webpart与custom webpart的联动方法

2012-12-23 20:01 363 查看
前面我们提到关于sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties,这次主要是要实现,如果让一个webpart给另一个webpart传值。在sharepoint中,有提供了一种方法,就是用[ConnectionProvider("WebpartConnectionProvider")]
和[ConnectionConsumer("WebpartConnectionConsumer")],其中的ConnectionProvider这个属性是数据提供着,参数值代表显示名称;ConnectionConsumer这个属性是数据接收者,参数值也是代表显示名称。

实现这样的功能,有一下几个步骤。

1。新建一个Interface接口,IGetName.cs

 public interface IGetName

    {

        string Name { get; set; }

    }

2。新建两个个可视化部件,WebpartConnectionProvider和WebpartConnectionConsumer

WebpartConnectionProvider.ascx

<asp:TextBox ID="txtstr" runat="server"></asp:TextBox>

<asp:Button ID="BtnProvider"

    runat="server" Text="发送" onclick="BtnProvider_Click" />

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

namespace WebpartBarTest.WebpartConnectionProvider

{

    public partial class WebpartConnectionProviderUserControl : UserControl

    {

        public WebpartConnectionProvider WebPart { get; set; }

        protected void Page_Load(object sender, EventArgs e)

        {

           

        }

        protected void BtnProvider_Click(object sender, EventArgs e)

        {

            WebPart.Name = txtstr.Text;

        } 

    }



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.WebpartConnectionProvider

{

    [ToolboxItemAttribute(false)]

    public class WebpartConnectionProvider : WebPart, IGetName

    {

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

        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WebpartBarTest/WebpartConnectionProvider/WebpartConnectionProviderUserControl.ascx";

        protected override void CreateChildControls()

        {

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

            if (control != null)

            {

                control.WebPart = this;

            }

           

            Controls.Add(control);

        }

        string _Name = "";

        public string Name

        {

            get { return _Name; }

            set { _Name = value; }

        }

        [ConnectionProvider("WebpartConnectionProvider")]

        public IGetName GetName()

        {

            return this as IGetName;

        }

       

    }

}

WebpartConnectionConsumer.ascx

传输过来的值是:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

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.WebpartConnectionConsumer

{

    [ToolboxItemAttribute(false)]

    public class WebpartConnectionConsumer : WebPart

    {

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

        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WebpartBarTest/WebpartConnectionConsumer/WebpartConnectionConsumerUserControl.ascx";

        public Label lbl;

        protected override void CreateChildControls()

        {

            Control control = Page.LoadControl(_ascxPath);

            lbl = (Label)control.FindControl("Label1");

            if (providerWebPart != null)

            {

                lbl.Text =providerWebPart.Name;

            }

            Controls.Add(control);

        }

        public IGetName providerWebPart;

        [ConnectionConsumer("WebpartConnectionConsumer")]

        public void ReceiveName(IGetName provider)

        {

            providerWebPart = provider;

        }

    }

}

3。部署到网站上。在页面上添加这两个webpart.如下图:



4。配置webpart连接,如下图,选择连接,发送对象,将consumer勾上。



5。保存编辑页面,如下图



接下来我们再文本框输入:广州京微信息科技有限公司,点击发送,把这个值,传给Consumer这个webpart接收,如下图:



如果我们继续数据其它的值,例如http://www.kingwi.com ,如下图:



这次主要是记录一下,如何用webpart给另一个webpart提供参数值,也就是我们常说的,webpart与webpart联动,连接。

广州京微信息科技有限公司,微软sharepoint解决方案提供商。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐