WebPartManager.GetProviderConnectionPoints(WebPart) 方法

定义

检索 ProviderConnectionPoint 对象的集合,这些对象可作为来自如下服务器控件的连接点:该服务器控件正在作为 Web 部件连接中的提供者。

public:
 virtual System::Web::UI::WebControls::WebParts::ProviderConnectionPointCollection ^ GetProviderConnectionPoints(System::Web::UI::WebControls::WebParts::WebPart ^ webPart);
public virtual System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection GetProviderConnectionPoints (System.Web.UI.WebControls.WebParts.WebPart webPart);
abstract member GetProviderConnectionPoints : System.Web.UI.WebControls.WebParts.WebPart -> System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection
override this.GetProviderConnectionPoints : System.Web.UI.WebControls.WebParts.WebPart -> System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection
Public Overridable Function GetProviderConnectionPoints (webPart As WebPart) As ProviderConnectionPointCollection

参数

webPart
WebPart

作为连接中的提供者的服务器控件。

返回

ProviderConnectionPointCollection

包含提供者中的所有连接点的 ProviderConnectionPointCollection

例外

webPartnull

示例

下面的代码示例演示如何使用 GetProviderConnectionPoints 方法。

此示例有四个部分:

  • 一个用户控件,可用于更改Web 部件页上的显示模式。

  • 包含两个可连接的自定义 WebPart 控件和一个 <asp:webpartmanager> 元素的网页。

  • 包含两个自定义 WebPart 控件和一个自定义接口的源代码文件。

  • 说明示例在浏览器中的工作原理。

代码示例的第一部分是用于更改显示模式的用户控件。 可以从类概述的“示例”部分获取用户控件的 WebPartManager 源代码。 有关显示模式和用户控件工作原理的详细信息,请参阅演练:更改Web 部件页上的显示模式

网页的声明性标记包含 Register 用户控件和自定义控件的指令。 有一个元素、一个<asp:webpartmanager>``<asp:webpartzone>元素来包含自定义控件和一个<asp:connectionszone>元素。 请注意,在 Page_Load 该方法中,代码检查连接是否已存在,如果不存在,则定义提供程序、使用者及其各自的连接点,然后将新连接添加到属性引用 StaticConnections 的静态连接集。 请注意, ProviderConnectionPointCollection 通过使用该方法检索 GetProviderConnectionPoints 的对象随后会传递给 CanConnectWebParts 方法,以确定是否可以创建两个控件之间的连接。

<%@ 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 Page_Load(object sender, EventArgs e)
  {
    
    // Define provider, consumer, and connection points.
    WebPart provider = mgr.WebParts["zip1"];
    ProviderConnectionPoint provConnPoint =
      mgr.GetProviderConnectionPoints(provider)["ZipCodeProvider"];
    WebPart consumer = mgr.WebParts["weather1"];
    ConsumerConnectionPoint consConnPoint =
      mgr.GetConsumerConnectionPoints(consumer)["ZipCodeConsumer"];
    
    // Check whether the connection already exists.
    if (mgr.CanConnectWebParts(provider, provConnPoint,
      consumer, consConnPoint))
    {
      // Create a new static connection.
      WebPartConnection conn = new WebPartConnection();
      conn.ID = "staticConn1";
      conn.ConsumerID = "weather1";
      conn.ConsumerConnectionPointID = "ZipCodeConsumer";
      conn.ProviderID = "zip1";
      conn.ProviderConnectionPointID = "ZipCodeProvider";
      mgr.StaticConnections.Add(conn);
    }
 }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="mgr" runat="server" />   
    <div>
      <uc1:DisplayModeMenuCS ID="displaymode1" 
        runat="server" />
      <!-- Reference consumer and provider controls 
           in a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <!-- Add a ConnectionsZone so users can connect 
           controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" 
        runat="server" />
    </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 Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    ' Define provider, consumer, and connection points.
    Dim provider As WebPart = mgr.WebParts("zip1")
    Dim provConnPoint As ProviderConnectionPoint = _
      mgr.GetProviderConnectionPoints(provider)("ZipCodeProvider")
    Dim consumer As WebPart = mgr.WebParts("weather1")
    Dim consConnPoint As ConsumerConnectionPoint = _
      mgr.GetConsumerConnectionPoints(consumer)("ZipCodeConsumer")
    
    ' Check whether the connection already exists.
    If mgr.CanConnectWebParts(provider, provConnPoint, _
      consumer, consConnPoint) Then
      ' Create a new static connection.
      Dim conn As New WebPartConnection()
      conn.ID = "staticConn1"
      conn.ConsumerID = "weather1"
      conn.ConsumerConnectionPointID = "ZipCodeConsumer"
      conn.ProviderID = "zip1"
      conn.ProviderConnectionPointID = "ZipCodeProvider"
      mgr.StaticConnections.Add(conn)
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="mgr" runat="server" />   
    <div>
      <uc1:DisplayModeMenuVB ID="displaymode1" 
        runat="server" />
      <!-- Reference consumer and provider controls 
           in a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <!-- Add a ConnectionsZone so users can connect 
           controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" 
        runat="server" />
    </div>
    </form>
</body>
</html>

示例的第三部分是控件的源代码。 可以从类概述的 WebPartManager “示例”部分获取此代码以及编译代码的说明。

在浏览器中加载网页后,单击 “显示模式”下拉列表控件,然后选择 连接 切换页面以连接模式。 连接模式使用<asp:connectionszone>元素来创建控件之间的连接。 在连接模式下,单击 邮政编码 控件标题栏中的向下箭头以激活其谓词菜单,然后单击 连接。 显示连接用户界面 (UI) 后,请注意,方法中包含的 Page_Load 代码已创建连接。 如果在以后的浏览器会话中返回到此页面,则会建立此静态连接,并且每次加载页面时都无需重新创建此静态连接。

注解

Web 部件连接始终涉及两个控件,一个控件充当数据提供程序,另一个控件充当数据的使用者。 每个控件必须具有一个或多个定义为连接点的方法。 对于提供程序控件,其连接点是 ProviderConnectionPoint 对象。

提供程序必须始终至少有一个连接点才能建立连接。 该方法 GetProviderConnectionPoints 检查提供程序控件并检索其所有连接点的集合。 检索提供程序连接点是形成Web 部件连接的必要步骤。

适用于

另请参阅