WebPartManager.StaticConnections 屬性

定義

取得網頁上定義為靜態連接之所有 WebPartConnection 物件集合的參考。

public:
 property System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ StaticConnections { System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.WebParts.WebPartConnectionCollection StaticConnections { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.StaticConnections : System.Web.UI.WebControls.WebParts.WebPartConnectionCollection
Public ReadOnly Property StaticConnections As WebPartConnectionCollection

屬性值

WebPartConnectionCollection,包含頁面上的所有靜態連接。

屬性

範例

下列程式碼範例示範屬性的程式 StaticConnections 設計用法。

程式碼範例有四個部分:

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

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

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

  • 說明範例如何在瀏覽器中運作。

下列程式碼只包含範例的網頁部分。 您也必須從類別概觀的 WebPartManager Example 區段取得範例的前兩個部分:自訂使用者控制項和自訂控制項的原始程式碼和介面。 該主題也會說明編譯控制項的選項 WebPart

程式碼範例的第三個部分是網頁。 頁面的宣告式標記包含 Register 使用者控制項和自訂控制項的指示詞。 有專案 <asp:webpartmanager><asp:webpartzone> 要包含自訂控制項的專案,以及專案 <asp:connectionszone> 。 請注意,在 方法中 Page_Load ,程式碼會檢查連線是否已存在,如果不存在,則定義提供者、取用者及其各自的連接點,然後將新的連接新增至 屬性所 StaticConnections 參考的靜態連接集。

<%@ 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>

在瀏覽器中載入網頁之後,按一下 [ 顯示模式 ] 下拉式清單控制項,然後選取 [ 連線 ] 以切換頁面以連線模式。 連線模式會 <asp:connectionszone> 使用 元素來讓您建立控制項之間的連線。 在連線模式中,按一下 [郵遞區號 ] 控制項標題列中的向下箭號,以啟動其動詞功能表,然後按一下 [ 連接]。 出現連接使用者介面 (UI) 之後,請注意方法中包含的 Page_Load 程式碼已經建立連線。 如果您在稍後的瀏覽器會話中返回此頁面,就會建立此靜態連線,而且每次載入頁面時都不需要重新建立。

備註

控制項 StaticConnections 會使用 WebPartManager 屬性來追蹤和管理頁面上的所有靜態連線。 靜態連接與動態連接相反,不需要在每次轉譯頁面時新增至頁面。

這個屬性所參考的集合包含頁面上存在的所有靜態連接,不論它們是以程式設計方式建立,還是以 <asp:webpartconnection> 頁面標記中的專案指定。

適用於

另請參閱