WebPartManager.Connections 属性

定义

获取对网页上所有当前连接的集合的引用。

public:
 property System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ Connections { System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.WebParts.WebPartConnectionCollection Connections { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Connections : System.Web.UI.WebControls.WebParts.WebPartConnectionCollection
Public ReadOnly Property Connections As WebPartConnectionCollection

属性值

WebPartConnectionCollection

包含一组 WebPartConnectionCollection 对象的 WebPartConnection

属性

示例

下面的代码示例演示了控件的 WebPartManager 声明性和编程用法。

代码示例包含四个部分:

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

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

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

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

以下代码仅包含示例的网页部分。 还需要自定义用户控件和上述自定义控件的源代码。 从类概述的 WebPartManager “示例”部分获取这两项。

以下网页代码演示如何以编程方式使用 Connections 属性来获取页面上的当前连接计数。 请注意,在 <script> 标记部分中,用于处理控件的两个事件 WebPartManager 的代码访问 Connections 属性以获取计数。

<%@ 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">
 
  private void UpdateLabelData(int wpCount, int connCount)
  {
    Label1.Text = "WebPart Control Count:  " + wpCount.ToString();
    Label2.Text = "Connections Count: " + connCount.ToString();
  }

  protected void WebPartManager1_WebPartsConnected(object sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }

  protected void WebPartManager1_WebPartsDisconnected(object sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }
  
</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="WebPartManager1" runat="server"  
        OnWebPartsConnected="WebPartManager1_WebPartsConnected" 
        OnWebPartsDisconnected="WebPartManager1_WebPartsDisconnected" />
    <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 />
      <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
      <br />
      <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
      <!-- 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 WebPartManager1_WebPartsConnected( _
    ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.WebParts.WebPartConnectionsEventArgs)
    
    UpdateLabelData(WebPartManager1.WebParts.Count, _
      WebPartManager1.Connections.Count)
    
  End Sub

  Protected Sub WebPartManager1_WebPartsDisconnected( _
    ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.WebParts.WebPartConnectionsEventArgs)
    
    UpdateLabelData(WebPartManager1.WebParts.Count, _
      WebPartManager1.Connections.Count)
    
  End Sub
  
  Private Sub UpdateLabelData(ByVal wpCount As Integer, _
    ByVal connCount As Integer)
    
    Label1.Text = "WebPart Control Count:  " & wpCount.ToString()
    Label2.Text = "Connections Count: " & connCount.ToString()
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="WebPartManager1" runat="server" OnWebPartsConnected="WebPartManager1_WebPartsConnected" OnWebPartsDisconnected="WebPartManager1_WebPartsDisconnected" />
    <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 />
      <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
      <br />
      <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
      <!-- Add a ConnectionsZone so users can connect controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server" />
    </div>
    </form>
</body>
</html>

在浏览器中加载网页后,单击 “显示模式”下拉列表控件,然后选择 连接 将页面切换为连接模式。 连接模式使用<asp:connectionszone>元素在控件之间创建连接。 在连接模式下,单击 邮政编码 控件标题栏中的向下箭头以激活其谓词菜单,然后单击 连接。 (UI) 显示连接用户界面后,单击“ 创建与使用者”链接的连接 。 此时会显示一个包含下拉列表控件的单元格。 在下拉列表中选择 “天气控制”,然后单击 连接 以完成两个控件的连接。 单击“ 关闭”,然后使用 “显示模式 ”下拉列表将页面返回到普通浏览模式。 请注意,标签现在显示连接数和控件数 WebPart 。 如果现在返回到连接模式并断开两个控件的连接,则返回浏览模式时,标签的内容应更新,并且不应有连接。

注解

Connections 属性提供了一种方法来访问页面上的当前连接集。 集合本身是只读的,想要从集合中操作特定连接的开发人员应使用WebPartManager和等ConnectWebPartsDisconnectWebParts方法。

适用于

另请参阅