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,其中包含 WebPartConnection 物件的集合。

屬性

範例

下列程式碼範例示範控制項的 WebPartManager 宣告式和程式設計用法。

程式碼範例有四個部分:

  • 使用者控制項,可讓您變更網頁元件頁面上的顯示模式。

  • 網頁,包含兩個可連接的自訂 WebPart 控制項,以及 一個 <asp:webpartmanager> 元素。

  • 原始程式碼檔案,其中包含兩個自訂 WebPart 控制項和自訂介面。

  • 說明範例在瀏覽器中的運作方式。

下列程式碼只包含範例的網頁部分。 您也需要上述自訂控制項的自訂使用者控制項和原始程式碼。 從類別概觀的 WebPartManager Example 區段取得這兩個專案。

下列網頁程式碼示範如何以程式設計方式使用 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屬性提供一種方式來存取頁面上目前的連線集。 集合本身是唯讀的,而想要從集合操作特定連線的開發人員應該使用 WebPartManagerDisconnectWebParts 之類的 ConnectWebParts 方法。

適用於

另請參閱