一步一步webpart-两个WebPart之间交换数据(8)
说明:
本项目中涉及webpart数据提供者和数据使用者两个类
提供者可以向使用者提供:
一项数据(Cell)
一行数据(Row)
一个表的数据(List)
查询过滤器(Filter)
参数(Parameters)
创建项目:
1.新建一个webpartLibrary项目,取名为WebPartLibrary5;
2.删除自动产生的webpart1.cs和webpart1.dwp;
3.新建一个"provider web part"取名为CityProvider.cs,代码如下:

数据提供者
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Security;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebPartPages.Communication;

namespace WebPartLibrary5


{

/**//// <summary>
/// Description for CityProvider.
/// This Web Part implements the ICellProvider interface.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:CityProvider runat=server></{0}:CityProvider>"),
XmlRoot(Namespace="WebPartLibrary5")]
public class CityProvider : Microsoft.SharePoint.WebPartPages.WebPart, ICellProvider

{
//*********下拉框控件***************
private DropDownList cityList = new DropDownList();
// Event required by ICellProvider
public event CellProviderInitEventHandler CellProviderInit;
// Event required by ICellProvider
public event CellReadyEventHandler CellReady;
// Cell information
private string cellName = "Cell Data";

const string defaulttext = "";

private string text = defaulttext;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaulttext),
Description(""),
FriendlyName("Text"),
WebPartStorageAttribute(Storage.Personal)]
public string Text

{
get

{
return text;
}

set

{
text = value;
}
}

//************重写的“OnLoad()”方法,给下拉框绑定数据*************
protected override void OnLoad(EventArgs e)

{
if (cityList.Items.Count == 0)

{

cityList.DataSource = new String[]
{"北京", "上海", "深圳"};
cityList.DataBind();
}
this.Controls.Add( cityList );
cityList.AutoPostBack = true;
}

/**//// <summary>
/// Register interfaces.
/// </summary>
public override void EnsureInterfaces()

{
try

{
//************更改后的参数*******************
RegisterInterface("CellProvider_WPQ_",
"ICellProvider",
WebPart.UnlimitedConnections,
ConnectionRunAt.Server,
this,
"CellProviderInterface_WPQ_",
"提供一个城市名称给:",
"提供一项包含了城市名称的数据");

}
catch(SecurityException)

{
}
}


/**//// <summary>
/// This method is called by the framework to determine whether a part
/// can be run on the client or server based on the current
/// configuration.
/// </summary>
public override ConnectionRunAt CanRunAt()

{
return ConnectionRunAt.Server;
}


/**//// <summary>
/// Notification to the Web Part that it has been connected
/// </summary>
/// <param name="interfaceName">Unique name of the interface that is being connected</param>
/// <param name="connectedPart">Reference to the other Web Part that is being connected to</param>
/// <param name="connectedInterfaceName">Unique name of the interface on the other Web Part</param>
/// <param name="runAt">Where the interface should execute</param>
public override void PartCommunicationConnect(string interfaceName,
WebPart connectedPart,
string connectedInterfaceName,
ConnectionRunAt runAt)

{
EnsureChildControls();
}


/**//// <summary>
/// In this method, a part should fire any connection init events.
/// </summary>
public override void PartCommunicationInit()

{
if (CellProviderInit != null)

{
CellProviderInitEventArgs cellProviderInitArgs = new CellProviderInitEventArgs();
cellProviderInitArgs.FieldName = cellName;
CellProviderInit(this, cellProviderInitArgs);
}
}

//*******************传递下拉框选中项的数据************************

/**//// <summary>
/// In this method, a part can fire any events that it requires to.
/// </summary>
public override void PartCommunicationMain()

{
if (CellReady != null)

{
// 创建要传递出去的数据
CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
cellReadyArgs.Cell = cityList.SelectedItem.Text;

// 通过CellReady这个事件,将数据传递给使用者
CellReady( this, cellReadyArgs );

}
}


/**//// <summary>
/// The CellConsumerInit event handler
/// The Consumer part will fire this event during its PartCommunicationInit phase.
/// </summary>
/// <param name="sender">Consumer Web Part</param>
/// <param name="cellConsumerInitArgs">The args passed by the Consumer</param>
public void CellConsumerInit(object sender, CellConsumerInitEventArgs cellConsumerInitArgs)

{
// This is where the Provider part can identify what type of "Cell" the Consumer
// was expecting/requesting.
}

//*******************输出这个下拉框控件的内容***************************

/**//// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)

{
cityList.RenderControl(output);
}
}
}
4.新建CityProvider的部署文件CityProvider.dwp,代码如下:

cityprovider的部署文件
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>CityProvider</Title>
<Description>一个提供城市名称的WebPart</Description>
<Assembly>WebPartLibrary5</Assembly>
<TypeName>WebPartLibrary5.CityProvider</TypeName>
<!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>

5.新建一个"consumer web part"取名为CityConsumer.cs,代码如下:

数据使用者
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Security;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebPartPages.Communication;

namespace WebPartLibrary5


{

/**//// <summary>
/// Description for CityConsumer.
/// This Web Part implements the ICellConsumer interface.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:CityConsumer runat=server></{0}:CityConsumer>"),
XmlRoot(Namespace="WebPartLibrary5")]
public class CityConsumer : Microsoft.SharePoint.WebPartPages.WebPart , ICellConsumer

{
// Event required by ICellConsumer
public event CellConsumerInitEventHandler CellConsumerInit;

// Keep a count of ICellConsumer connections.
private int cellConnectedCount = 0;

// Cell information
private string cellName = "Cell Data";
const string defaulttext = "";

private string text = defaulttext;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaulttext),
Description(""),
FriendlyName("Text"),
WebPartStorageAttribute(Storage.Personal)]
public string Text

{
get

{
return text;
}

set

{
text = value;
}
}


/**//// <summary>
/// Register interfaces.
/// </summary>
public override void EnsureInterfaces()

{
try

{
//************更改后的参数*******************
RegisterInterface
("CellConsumer_WPQ_",
"ICellConsumer",
WebPart. LimitOneConnection,
ConnectionRunAt.Server,
this,
"CellConsumer_WPQ_",
"从
处接收一个城市名称",
"获取一项包含了城市名称的数据");
}
catch(SecurityException)

{
}
}


/**//// <summary>
/// This method is called by the framework to determine whether a part
/// can be run on the client or server based on the current
/// configuration.
/// </summary>
public override ConnectionRunAt CanRunAt()

{
return ConnectionRunAt.Server;
}


/**//// <summary>
/// Notification to the Web Part that it has been connected
/// </summary>
/// <param name="interfaceName">Unique name of the interface that is being connected</param>
/// <param name="connectedPart">Reference to the other Web Part that is being connected to</param>
/// <param name="connectedInterfaceName">Unique name of the interface on the other Web Part</param>
/// <param name="runAt">Where the interface should execute</param>
public override void PartCommunicationConnect(string interfaceName,
WebPart connectedPart,
string connectedInterfaceName,
ConnectionRunAt runAt)

{
EnsureChildControls();
if (interfaceName == "CellConsumer_WPQ_")

{
cellConnectedCount++;
}
}


/**//// <summary>
/// In this method, a part should fire any connection init events.
/// </summary>
public override void PartCommunicationInit()

{
if(cellConnectedCount > 0)

{
if (CellConsumerInit != null)

{
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
CellConsumerInit(this, cellConsumerInitArgs);
}
}
}


/**//// <summary>
/// This method is called by the Authoring environment for all
/// the initial data required for creating a connection.
/// </summary>
/// <param name="interfacename">Name of interface that the framework is
/// requesting information on</param>
/// <returns>An EventArgs object such as CellProviderInitArgs</returns>
public override InitEventArgs GetInitEventArgs(string interfaceName)

{
if (interfaceName == "CellConsumer_WPQ_")

{
EnsureChildControls();
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
return(cellConsumerInitArgs);
}
else

{
return(null);
}
}


/**//// <summary>
/// The CellProviderInit event handler
/// The Provider part will fire this event during its PartCommunicationInit phase.
/// </summary>
/// <param name="sender">Provider Web Part</param>
/// <param name="cellProviderInitArgs">The args passed by the Provider</param>
public void CellProviderInit(object sender, CellProviderInitEventArgs cellProviderInitArgs)

{
// This is where the Consumer part can identify what type of "Cell" the Provider
// will be sending.
}

//***************接收Provider WebPart传送过来的数据,并赋值给自动生成的Text属性*******************

/**//// <summary>
/// The CellReady event handler
/// The Provider part will fire this event during its PartCommunicationMain phase.
/// </summary>
/// <param name="sender">Provider Web Part</param>
/// <param name="cellReadyArgs">The args passed by the Provider</param>
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)

{
if(cellReadyArgs.Cell != null)

{
Text = "你选择的是:" + cellReadyArgs.Cell.ToString();
}
}


/**//// <summary>
/// Render this Web Part control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)

{
output.Write(SPEncode.HtmlEncode(Text));
}
}
}
6.新建CityConsumer的部署文件CityConsumer.dwp,代码如下:

数据使用者的配置文件
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>CityConsumer</Title>
<Description>一个接收城市名称的WebPart</Description>
<Assembly>WebPartLibrary5</Assembly>
<TypeName>WebPartLibrary5.CityConsumer</TypeName>
<!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>

7.部署两个webpart
我是通过Cabman2003部署的,请参见一步一步webpart-将WebPart部署到SPS服务器(7)
8.连接数据提供者于数据使用者
*****点击CityProvider WebPart右上方的菜单按钮,选择“连接、提供一个城市名称给:""CityConsumer”
或
*****点击CityConsumer WebPart右上角的菜单按钮,选择“连接、从…处接收一个城市名称""CityProvider”
即将两个webpart链接起来。
本项目中涉及webpart数据提供者和数据使用者两个类
提供者可以向使用者提供:
一项数据(Cell)
一行数据(Row)
一个表的数据(List)
查询过滤器(Filter)
参数(Parameters)
创建项目:
1.新建一个webpartLibrary项目,取名为WebPartLibrary5;
2.删除自动产生的webpart1.cs和webpart1.dwp;
3.新建一个"provider web part"取名为CityProvider.cs,代码如下:
































































































































































































































































































































































































































































































我是通过Cabman2003部署的,请参见一步一步webpart-将WebPart部署到SPS服务器(7)
8.连接数据提供者于数据使用者
*****点击CityProvider WebPart右上方的菜单按钮,选择“连接、提供一个城市名称给:""CityConsumer”
或
*****点击CityConsumer WebPart右上角的菜单按钮,选择“连接、从…处接收一个城市名称""CityProvider”
即将两个webpart链接起来。
-
- DeepSought
- 探索AI融入千行百业与日常生活
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述