使用英语阅读

通过


WebPartManager.GetConsumerConnectionPoints(WebPart) 方法

定义

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

C#
public virtual System.Web.UI.WebControls.WebParts.ConsumerConnectionPointCollection GetConsumerConnectionPoints(System.Web.UI.WebControls.WebParts.WebPart webPart);

参数

webPart
WebPart

作为连接中的使用者的服务器控件。

返回

包含使用者中的所有连接点的 ConsumerConnectionPointCollection

例外

webPartnull

示例

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

该代码示例包含四个部分:

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

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

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

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

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

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

ASP.NET (C#)
<%@ 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>

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

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

注解

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

使用者必须始终至少有一个连接点才能建立连接。 方法 GetConsumerConnectionPoints 检查使用者控件并检索其所有连接点的集合。 如果控件 WebPart 没有连接点,该方法将返回一个空集合。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅