一步一步webpart-两个WebPart之间交换数据(8)

本文介绍如何使用 SharePoint WebPart 实现数据提供者与数据使用者之间的数据交互过程,包括创建 WebPart、部署文件及实现数据传递的具体步骤。
说明:
本项目中涉及webpart数据提供者和数据使用者两个类
  提供者可以向使用者提供:
一项数据(Cell)
一行数据(Row)
一个表的数据(List)
查询过滤器(Filter)
参数(Parameters)
创建项目:
1.新建一个webpartLibrary项目,取名为WebPartLibrary5;
2.删除自动产生的webpart1.cs和webpart1.dwp;
3.新建一个"provider web part"取名为CityProvider.cs,代码如下:
ContractedBlock.gif ExpandedBlockStart.gif 数据提供者
None.gifusing System;
None.gif
using System.ComponentModel;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Xml.Serialization;
None.gif
using System.Security;
None.gif
using System.Security.Permissions;
None.gif
using Microsoft.SharePoint;
None.gif
using Microsoft.SharePoint.Utilities;
None.gif
using Microsoft.SharePoint.WebPartPages;
None.gif
using Microsoft.SharePoint.WebPartPages.Communication;
None.gif
None.gif
namespace WebPartLibrary5
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Description for CityProvider.
InBlock.gif    
/// This Web Part implements the ICellProvider interface.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Text"),
InBlock.gif        ToolboxData(
"<{0}:CityProvider runat=server></{0}:CityProvider>"),
InBlock.gif        XmlRoot(Namespace
="WebPartLibrary5")]
InBlock.gif    
public class CityProvider : Microsoft.SharePoint.WebPartPages.WebPart, ICellProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//*********下拉框控件***************
InBlock.gif
        private DropDownList cityList = new DropDownList();
InBlock.gif        
// Event required by ICellProvider
InBlock.gif
        public event CellProviderInitEventHandler CellProviderInit;    
InBlock.gif        
InBlock.gif        
// Event required by ICellProvider
InBlock.gif
        public event CellReadyEventHandler CellReady;        
InBlock.gif            
InBlock.gif        
// Cell information
InBlock.gif
        private string cellName = "Cell Data";
InBlock.gif
InBlock.gif        
const string defaulttext = "";
InBlock.gif
InBlock.gif        
private string text = defaulttext;
InBlock.gif    
InBlock.gif        [Browsable(
true), 
InBlock.gif            Category(
"Miscellaneous"), 
InBlock.gif            DefaultValue(defaulttext),
InBlock.gif            Description(
""),
InBlock.gif            FriendlyName(
"Text"),
InBlock.gif            WebPartStorageAttribute(Storage.Personal)] 
InBlock.gif        
public string Text 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//************重写的“OnLoad()”方法,给下拉框绑定数据*************
InBlock.gif
        protected override void OnLoad(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (cityList.Items.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                cityList.DataSource 
= new String[] dot.gif{"北京""上海""深圳"};
InBlock.gif                cityList.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.Controls.Add( cityList );
InBlock.gif            cityList.AutoPostBack 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    Register interfaces.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void EnsureInterfaces()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//************更改后的参数*******************
InBlock.gif
                RegisterInterface("CellProvider_WPQ_",
InBlock.gif                
"ICellProvider",
InBlock.gif                WebPart.UnlimitedConnections,
InBlock.gif                ConnectionRunAt.Server,
InBlock.gif                
this,
InBlock.gif                
"CellProviderInterface_WPQ_",
InBlock.gif                
"提供一个城市名称给:",
InBlock.gif                
"提供一项包含了城市名称的数据");
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(SecurityException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    This method is called by the framework to determine whether a part 
InBlock.gif        
///    can be run on the client or server based on the current
InBlock.gif        
///    configuration.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override ConnectionRunAt CanRunAt()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ConnectionRunAt.Server;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    Notification to the Web Part that it has been connected
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="interfaceName">Unique name of the interface that is being connected</param>
InBlock.gif        
/// <param name="connectedPart">Reference to the other Web Part that is being connected to</param>
InBlock.gif        
/// <param name="connectedInterfaceName">Unique name of the interface on the other Web Part</param>
ExpandedSubBlockEnd.gif        
/// <param name="runAt">Where the interface should execute</param>

InBlock.gif        public override void PartCommunicationConnect(string interfaceName,
InBlock.gif            WebPart connectedPart,
InBlock.gif            
string connectedInterfaceName,
InBlock.gif            ConnectionRunAt runAt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EnsureChildControls(); 
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    In this method, a part should fire any connection init events.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void PartCommunicationInit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (CellProviderInit != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CellProviderInitEventArgs cellProviderInitArgs 
= new CellProviderInitEventArgs();
InBlock.gif                cellProviderInitArgs.FieldName 
= cellName;
InBlock.gif                CellProviderInit(
this, cellProviderInitArgs);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//*******************传递下拉框选中项的数据************************
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//// <summary>
InBlock.gif        
/// In this method, a part can fire any events that it requires to.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void PartCommunicationMain()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (CellReady != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 创建要传递出去的数据
InBlock.gif
                CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
InBlock.gif                cellReadyArgs.Cell 
= cityList.SelectedItem.Text;
InBlock.gif
InBlock.gif                
// 通过CellReady这个事件,将数据传递给使用者
InBlock.gif
                CellReady( this, cellReadyArgs );
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The CellConsumerInit event handler
InBlock.gif        
/// The Consumer part will fire this event during its PartCommunicationInit phase.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender">Consumer Web Part</param>
ExpandedSubBlockEnd.gif        
/// <param name="cellConsumerInitArgs">The args passed by the Consumer</param>

InBlock.gif        public void CellConsumerInit(object sender, CellConsumerInitEventArgs cellConsumerInitArgs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// This is where the Provider part can identify what type of "Cell" the Consumer
InBlock.gif            
// was expecting/requesting.
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
//*******************输出这个下拉框控件的内容***************************
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//// <summary>
InBlock.gif        
/// Render this control to the output parameter specified.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> The HTML writer to write out to </param>

InBlock.gif        protected override void RenderWebPart(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cityList.RenderControl(output);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
4.新建CityProvider的部署文件CityProvider.dwp,代码如下:
ContractedBlock.gif ExpandedBlockStart.gif cityprovider的部署文件
None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
None.gif    
<Title>CityProvider</Title>
None.gif    
<Description>一个提供城市名称的WebPart</Description>
None.gif    
<Assembly>WebPartLibrary5</Assembly>
None.gif    
<TypeName>WebPartLibrary5.CityProvider</TypeName>
None.gif    
<!-- Specify initial values for any additional base class or custom properties here. -->
None.gif
</WebPart>
None.gif
None.gif
5.新建一个"consumer web part"取名为CityConsumer.cs,代码如下:
ContractedBlock.gif ExpandedBlockStart.gif 数据使用者
None.gifusing System;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.ComponentModel;
None.gif
using System.Xml.Serialization;
None.gif
using System.Security;
None.gif
using System.Security.Permissions;
None.gif
using Microsoft.SharePoint;
None.gif
using Microsoft.SharePoint.Utilities;
None.gif
using Microsoft.SharePoint.WebPartPages;
None.gif
using Microsoft.SharePoint.WebPartPages.Communication;
None.gif
None.gif
namespace WebPartLibrary5
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Description for CityConsumer.
InBlock.gif    
/// This Web Part implements the ICellConsumer interface.
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    [DefaultProperty("Text"),
InBlock.gif        ToolboxData(
"<{0}:CityConsumer runat=server></{0}:CityConsumer>"),
InBlock.gif        XmlRoot(Namespace
="WebPartLibrary5")]
InBlock.gif    
public class CityConsumer : Microsoft.SharePoint.WebPartPages.WebPart , ICellConsumer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Event required by ICellConsumer
InBlock.gif
        public event CellConsumerInitEventHandler CellConsumerInit;
InBlock.gif
InBlock.gif        
// Keep a count of ICellConsumer connections.
InBlock.gif
        private int cellConnectedCount = 0
InBlock.gif
InBlock.gif        
// Cell information
InBlock.gif
        private string cellName = "Cell Data"
InBlock.gif        
InBlock.gif        
const string defaulttext = "";
InBlock.gif
InBlock.gif        
private string text = defaulttext;
InBlock.gif    
InBlock.gif        [Browsable(
true), 
InBlock.gif            Category(
"Miscellaneous"), 
InBlock.gif            DefaultValue(defaulttext),
InBlock.gif            Description(
""),
InBlock.gif            FriendlyName(
"Text"),
InBlock.gif            WebPartStorageAttribute(Storage.Personal)] 
InBlock.gif        
public string Text 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    Register interfaces.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void EnsureInterfaces()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//************更改后的参数*******************
InBlock.gif
                RegisterInterface
InBlock.gif                    (
"CellConsumer_WPQ_",
InBlock.gif                    
"ICellConsumer",
InBlock.gif                    WebPart. LimitOneConnection,
InBlock.gif                    ConnectionRunAt.Server,
InBlock.gif                    
this,
InBlock.gif                    
"CellConsumer_WPQ_",
InBlock.gif                    
"dot.gif处接收一个城市名称",
InBlock.gif                    
"获取一项包含了城市名称的数据");        
ExpandedSubBlockEnd.gif            }
                                                
InBlock.gif            
catch(SecurityException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    This method is called by the framework to determine whether a part 
InBlock.gif        
///    can be run on the client or server based on the current
InBlock.gif        
///    configuration.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override ConnectionRunAt CanRunAt()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ConnectionRunAt.Server;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    Notification to the Web Part that it has been connected
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="interfaceName">Unique name of the interface that is being connected</param>
InBlock.gif        
/// <param name="connectedPart">Reference to the other Web Part that is being connected to</param>
InBlock.gif        
/// <param name="connectedInterfaceName">Unique name of the interface on the other Web Part</param>
ExpandedSubBlockEnd.gif        
/// <param name="runAt">Where the interface should execute</param>

InBlock.gif        public override void PartCommunicationConnect(string interfaceName,
InBlock.gif            WebPart connectedPart,
InBlock.gif            
string connectedInterfaceName,
InBlock.gif            ConnectionRunAt runAt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EnsureChildControls(); 
InBlock.gif            
if (interfaceName == "CellConsumer_WPQ_")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cellConnectedCount
++;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    In this method, a part should fire any connection init events.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void PartCommunicationInit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(cellConnectedCount > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (CellConsumerInit != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CellConsumerInitEventArgs cellConsumerInitArgs 
= new CellConsumerInitEventArgs();
InBlock.gif                    cellConsumerInitArgs.FieldName 
= cellName;
InBlock.gif                    CellConsumerInit(
this, cellConsumerInitArgs);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    This method is called by the Authoring environment for all
InBlock.gif        
///    the initial data required for creating a connection.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="interfacename">Name of interface that the framework is
InBlock.gif        
/// requesting information on</param>
ExpandedSubBlockEnd.gif        
/// <returns>An EventArgs object such as CellProviderInitArgs</returns>

InBlock.gif        public override InitEventArgs GetInitEventArgs(string interfaceName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (interfaceName == "CellConsumer_WPQ_")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                CellConsumerInitEventArgs cellConsumerInitArgs 
= new CellConsumerInitEventArgs();    
InBlock.gif                cellConsumerInitArgs.FieldName 
= cellName;
InBlock.gif                
return(cellConsumerInitArgs);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return(null);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The CellProviderInit event handler
InBlock.gif        
/// The Provider part will fire this event during its PartCommunicationInit phase.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender">Provider Web Part</param>
ExpandedSubBlockEnd.gif        
/// <param name="cellProviderInitArgs">The args passed by the Provider</param>

InBlock.gif        public void CellProviderInit(object sender, CellProviderInitEventArgs cellProviderInitArgs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// This is where the Consumer part can identify what type of "Cell" the Provider
InBlock.gif            
// will be sending.
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
//***************接收Provider WebPart传送过来的数据,并赋值给自动生成的Text属性*******************
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//// <summary>
InBlock.gif        
/// The CellReady event handler
InBlock.gif        
/// The Provider part will fire this event during its PartCommunicationMain phase.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender">Provider Web Part</param>
ExpandedSubBlockEnd.gif        
/// <param name="cellReadyArgs">The args passed by the Provider</param>

InBlock.gif        public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(cellReadyArgs.Cell != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Text 
= "你选择的是:" + cellReadyArgs.Cell.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// Render this Web Part control to the output parameter specified.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> The HTML writer to write out to </param>

InBlock.gif        protected override void RenderWebPart(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            output.Write(SPEncode.HtmlEncode(Text));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
6.新建CityConsumer的部署文件CityConsumer.dwp,代码如下:
ContractedBlock.gif ExpandedBlockStart.gif 数据使用者的配置文件
None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
None.gif    
<Title>CityConsumer</Title>
None.gif    
<Description>一个接收城市名称的WebPart</Description>
None.gif    
<Assembly>WebPartLibrary5</Assembly>
None.gif    
<TypeName>WebPartLibrary5.CityConsumer</TypeName>
None.gif    
<!-- Specify initial values for any additional base class or custom properties here. -->
None.gif
</WebPart>
None.gif
None.gif
7.部署两个webpart
   我是通过Cabman2003部署的,请参见 一步一步webpart-将WebPart部署到SPS服务器(7)
8.连接数据提供者于数据使用者
  ***** 点击 CityProvider WebPart 右上方的菜单按钮,选择 “ 连接、提供一个城市名称给:"" CityConsumer ”
     或
  *****点击 CityConsumer WebPart 右上角的菜单按钮,选择 “ 连接、从 … 处接收 一个城市名称"" CityProvider ”
即将两个webpart链接起来 。


转载于:https://www.cnblogs.com/tenghoo/archive/2006/09/22/511770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值