WebPartManager.ConnectWebParts 方法

定义

WebPart 区域中的两个 WebPartZoneBase 控件(或其他能建立连接的服务器控件)之间创建一个连接。

重载

ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)

只使用对控件的引用和控件所指定的 WebPart 对象在两个 GenericWebPartConnectionPoint 控件之间创建连接。

ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer)

使用对两个 WebPartGenericWebPart 控件的引用、它们的指定 ConnectionPoint 对象和一个 WebPartTransformer 对象在这两个控件之间创建连接。

注解

方法 ConnectWebParts 在可以连接的任意两 WebPart 个控件之间形成连接。 在调用此方法以创建连接之前,可能还需要在条件检查中调用 CanConnectWebParts 方法,以确保控件满足形成连接的要求。

注意

还可以在不是 WebPart 控件的两个服务器控件之间创建连接。 通常,这两个控件必须是自定义服务器控件 (例如,继承自 WebControl 的控件或现有的 ASP.NET 服务器控件) ,以便可以添加所需的成员。 这些控件还必须满足下面指定的要求。

两个控件之间的任何类型的连接方案都必须满足以下要求才能进行连接:

  • 每个控件都驻留在一个 WebPartZoneBase 区域中, (它不必是同一区域) 。

  • 正如在 Web 部件控件集中实现的那样,连接中的提供程序控件将接口作为公共方法实现,该方法充当对提供程序的回调,并在方法上具有 ConnectionProvider 元数据属性,以将其标识为提供程序连接点。 GetProviderConnectionPoints由于检索提供程序连接点的方法是虚拟的,因此派生WebPartManager控件不必使用相同的元数据属性。

  • 正如在 Web 部件控件集中实现的那样,连接中的使用者控件还具有一个特殊方法,该方法使它能够获取对提供程序回调方法中公开的接口的引用,并且使用者在方法上具有 ConnectionConsumer 元数据属性,以将其标识为使用者连接点。 GetConsumerConnectionPoints由于检索使用者连接点的方法是虚拟的,因此派生WebPartManager控件不必使用相同的元数据属性。

  • 回调方法必须兼容,即使用者可以使用提供程序的回调方法中提供的接口类型 (这意味着使用者和提供程序可以直接) 共享数据,或者开发人员必须使用 WebPartTransformer 对象将提供程序中的数据转换为使用者可以使用的形式。

ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)

只使用对控件的引用和控件所指定的 WebPart 对象在两个 GenericWebPartConnectionPoint 控件之间创建连接。

public:
 System::Web::UI::WebControls::WebParts::WebPartConnection ^ ConnectWebParts(System::Web::UI::WebControls::WebParts::WebPart ^ provider, System::Web::UI::WebControls::WebParts::ProviderConnectionPoint ^ providerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPart ^ consumer, System::Web::UI::WebControls::WebParts::ConsumerConnectionPoint ^ consumerConnectionPoint);
public System.Web.UI.WebControls.WebParts.WebPartConnection ConnectWebParts (System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.ProviderConnectionPoint providerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPart consumer, System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint consumerConnectionPoint);
member this.ConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint -> System.Web.UI.WebControls.WebParts.WebPartConnection
Public Function ConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint) As WebPartConnection

参数

provider
WebPart

一个 WebPart 控件,负责向另一个连接控件提供数据。

providerConnectionPoint
ProviderConnectionPoint

用作连接回调方法的方法。 与 Web 部件控件集中实现的一样,这是 provider 中以 ConnectionProvider 元数据特性标记的一个公共方法。

consumer
WebPart

一个 WebPart 控件,负责接收来自 provider 的数据,然后处理或显示接收到的数据。

consumerConnectionPoint
ConsumerConnectionPoint

providerConnectionPoint 连接的方法,它接收连接的数据。 与 Web 部件控件集中实现的一样,这是 consumer 中以 ConnectionConsumer 元数据特性标记的一个公共方法。

返回

一个 WebPartConnection,包含连接所需的提供者和使用者的各种信息。

例外

动态集合的 WebPartManager 控件集合是只读的。

示例

下面的代码示例演示如何使用此方法以编程方式创建连接。 有关运行示例所需的完整代码,请参阅类概述的 WebPartManager “示例”部分。 在该示例中,你将需要用于更改页面上显示模式的用户控件的源代码,以及两个自定义 WebPart 控件的源代码。

承载这两个控件的网页的代码如下所示。 页面使用 Register 顶部的 指令来声明用户控件和自定义控件。 然后,在 元素中 <asp:webpartzone> 以声明方式引用自定义控件。 处理 Button1_Click 方法的代码使用 ConnectWebParts 方法在控件之间创建连接。

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuCS"
    Src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" 
    Assembly="ConnectionSampleCS"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    ProviderConnectionPoint provPoint = 
      mgr.GetProviderConnectionPoints(zip1)["ZipCodeProvider"];
    ConsumerConnectionPoint connPoint = 
      mgr.GetConsumerConnectionPoints(weather1)["ZipCodeConsumer"];
    WebPartConnection conn1 = mgr.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);
  }

  protected void mgr_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Button1.Visible = true;
    else
      Button1.Visible = false;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
      </asp:WebPartManager>
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuVB"
    Src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" 
    Assembly="ConnectionSampleVB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim provPoint As ProviderConnectionPoint = _
      mgr.GetProviderConnectionPoints(zip1)("ZipCodeProvider")
    Dim connPoint As ConsumerConnectionPoint = _
      mgr.GetConsumerConnectionPoints(weather1)("ZipCodeConsumer")
    mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)

  End Sub

  Protected Sub mgr_DisplayModeChanged (ByVal sender as Object, _
    ByVal e as WebPartDisplayModeEventArgs)

    If mgr.DisplayMode is WebPartManager.ConnectDisplayMode Then
    Button1.Visible = True
    Else
    Button1.Visible = False
    End If

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
      </asp:WebPartManager>
      <uc1:DisplayModeMenuVB ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </div>
    </form>
</body>
</html>

在浏览器中加载页面后,单击“ 连接 Web 部件控件 ”按钮以形成连接。 然后,可以在文本框中输入一些数据,然后单击“ 输入 5 位邮政编码 ”按钮,以表明控件已连接,并在第二个控件中输入的数据将在第二个控件中更新。

注解

当控件的连接点足够兼容且无需使用 WebPartTransformer 对象即可连接时,此重载用于连接控件。 调用方法的此重载时,它只是将调用传递给方法的其他重载版本,并为需要 WebPartTransformer 对象的参数传递null

尝试以编程方式连接两个控件时,可以在条件检查中使用 CanConnectWebParts 方法来确定这些控件是否可以直接连接。

另请参阅

适用于

ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer)

使用对两个 WebPartGenericWebPart 控件的引用、它们的指定 ConnectionPoint 对象和一个 WebPartTransformer 对象在这两个控件之间创建连接。

public:
 virtual System::Web::UI::WebControls::WebParts::WebPartConnection ^ ConnectWebParts(System::Web::UI::WebControls::WebParts::WebPart ^ provider, System::Web::UI::WebControls::WebParts::ProviderConnectionPoint ^ providerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPart ^ consumer, System::Web::UI::WebControls::WebParts::ConsumerConnectionPoint ^ consumerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPartTransformer ^ transformer);
public virtual System.Web.UI.WebControls.WebParts.WebPartConnection ConnectWebParts (System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.ProviderConnectionPoint providerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPart consumer, System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint consumerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPartTransformer transformer);
abstract member ConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint * System.Web.UI.WebControls.WebParts.WebPartTransformer -> System.Web.UI.WebControls.WebParts.WebPartConnection
override this.ConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint * System.Web.UI.WebControls.WebParts.WebPartTransformer -> System.Web.UI.WebControls.WebParts.WebPartConnection
Public Overridable Function ConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint, transformer As WebPartTransformer) As WebPartConnection

参数

provider
WebPart

一个 WebPart,它的角色是为另一个连接的控件提供数据。

providerConnectionPoint
ProviderConnectionPoint

provider 中的一个公共方法,它标有 ConnectionProvider 元数据特性,并用作连接的回调方法。

consumer
WebPart

一个 WebPart,它担任从 providertransformer 中接收数据然后对数据进行处理和显示的角色。

consumerConnectionPoint
ConsumerConnectionPoint

consumer 中的一个公共方法,它标有 ConnectionConsumer 元数据特性,并与 providerConnectionPoint 连接以接收连接的数据。

transformer
WebPartTransformer

一个 WebPartTransformer,它通过将数据从 provider 转换为 consumer 可以处理的格式来实现两个控件之间的连接。

返回

一个 WebPartConnection,它包含有关提供者、使用者以及连接所需的转换器的信息。

例外

连接已在 PreRender 中激活。

注解

当控件的连接点不兼容时,此重载用于连接控件。 当实现与作为其连接点不同的接口providerconsumer,会发生不兼容。 转换器将数据转换为可由 理解 consumer的类型。

另请参阅

适用于