WebPartConnection 類別

定義

提供讓兩個 WebPart 控制項建立連接的物件。 此類別無法獲得繼承。

public ref class WebPartConnection sealed
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public sealed class WebPartConnection
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))>]
type WebPartConnection = class
Public NotInheritable Class WebPartConnection
繼承
WebPartConnection
屬性

範例

下列程式碼範例示範如何在兩 WebPart 個控制項之間建立簡單的連接。 此範例示範三種方式來形成連接:以宣告方式,方法是將連線的標記放在網頁的標記中;以程式設計方式,方法是在程式碼中建立連線;透過 UI,將控制項放在 ConnectionsZone 頁面上,讓使用者能夠建立連線。

程式碼範例有四個部分:

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

  • 介面和兩 WebPart 個控制項的原始程式碼,做為連線的提供者和取用者。

  • 用來裝載所有控制項並執行程式碼範例的網頁。

  • 如何執行範例頁面的說明。

此程式碼範例的第一個部分是使用者控制項,可讓使用者變更網頁上的顯示模式。 將下列原始程式碼儲存至 .ascx 檔案,為它指定指派給 Src 這個使用者控制項之 Register 指示詞的 屬性的檔案名,這靠近主控網頁頂端。 如需此控制項中原始程式碼顯示模式和描述的詳細資訊,請參閱逐步解說 :變更網頁元件頁面上的顯示模式

<%@ control language="C#" classname="DisplayModeMenuCS"%>
<script runat="server">
  
 // Use a field to reference the current WebPartManager.
  WebPartManager _manager;

  void Page_Init(object sender, EventArgs e)
  {
    Page.InitComplete += new EventHandler(InitComplete);
  }  

  void InitComplete(object sender, System.EventArgs e)
  {
    _manager = WebPartManager.GetCurrentWebPartManager(Page);

    String browseModeName = WebPartManager.BrowseDisplayMode.Name;

    // Fill the dropdown with the names of supported display modes.
    foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
    {
      String modeName = mode.Name;
      // Make sure a mode is enabled before adding it.
      if (mode.IsEnabled(_manager))
      {
        ListItem item = new ListItem(modeName, modeName);
        DisplayModeDropdown.Items.Add(item);
      }
    }

    // If shared scope is allowed for this user, display the scope-switching
    // UI and select the appropriate radio button for the current user scope.
    if (_manager.Personalization.CanEnterSharedScope)
    {
      Panel2.Visible = true;
      if (_manager.Personalization.Scope == PersonalizationScope.User)
        RadioButton1.Checked = true;
      else
        RadioButton2.Checked = true;
    }
    
  }
 
  // Change the page to the selected display mode.
  void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;

    WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];
    if (mode != null)
      _manager.DisplayMode = mode;
  }

  // Set the selected item equal to the current display mode.
  void Page_PreRender(object sender, EventArgs e)
  {
    ListItemCollection items = DisplayModeDropdown.Items;
    int selectedIndex = 
      items.IndexOf(items.FindByText(_manager.DisplayMode.Name));
    DisplayModeDropdown.SelectedIndex = selectedIndex;
  }

  // Reset all of a user's personalization data for the page.
  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    _manager.Personalization.ResetPersonalizationState();
  }

  // If not in User personalization scope, toggle into it.
  protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.Scope == PersonalizationScope.Shared)
      _manager.Personalization.ToggleScope();
  }

  // If not in Shared scope, and if user is allowed, toggle the scope.
  protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.CanEnterSharedScope && 
        _manager.Personalization.Scope == PersonalizationScope.User)
      _manager.Personalization.ToggleScope();
  }
</script>
<div>
  <asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="230" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text=" Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" 
      AssociatedControlID="DisplayModeDropdown"/>
    <asp:DropDownList ID="DisplayModeDropdown" runat="server"  
      AutoPostBack="true" 
      Width="120"
      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
    <asp:LinkButton ID="LinkButton1" runat="server"
      Text="Reset User State" 
      ToolTip="Reset the current user's personalization data for the page."
      Font-Size="8" 
      OnClick="LinkButton1_Click" />
    <asp:Panel ID="Panel2" runat="server" 
      GroupingText="Personalization Scope"
      Font-Bold="true"
      Font-Size="8" 
      Visible="false" >
      <asp:RadioButton ID="RadioButton1" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
      <asp:RadioButton ID="RadioButton2" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        OnCheckedChanged="RadioButton2_CheckedChanged" />
    </asp:Panel>
  </asp:Panel>
</div>
<%@ control language="vb" classname="DisplayModeMenuVB"%>
<script runat="server">
  ' Use a field to reference the current WebPartManager.
  Dim _manager As WebPartManager

  Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    AddHandler Page.InitComplete, AddressOf InitComplete
  End Sub

  Sub InitComplete(ByVal sender As Object, ByVal e As System.EventArgs)
    _manager = WebPartManager.GetCurrentWebPartManager(Page)
      
    Dim browseModeName As String = WebPartManager.BrowseDisplayMode.Name
      
    ' Fill the dropdown with the names of supported display modes.
    Dim mode As WebPartDisplayMode
    For Each mode In _manager.SupportedDisplayModes
      Dim modeName As String = mode.Name
      ' Make sure a mode is enabled before adding it.
      If mode.IsEnabled(_manager) Then
        Dim item As New ListItem(modeName, modeName)
        DisplayModeDropdown.Items.Add(item)
      End If
    Next mode
      
    ' If shared scope is allowed for this user, display the scope-switching
    ' UI and select the appropriate radio button for the current user scope.
    If _manager.Personalization.CanEnterSharedScope Then
      Panel2.Visible = True
      If _manager.Personalization.Scope = PersonalizationScope.User Then
        RadioButton1.Checked = True
      Else
        RadioButton2.Checked = True
      End If
    End If
   
  End Sub

  ' Change the page to the selected display mode.
  Sub DisplayModeDropdown_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    Dim selectedMode As String = DisplayModeDropdown.SelectedValue   
    Dim mode As WebPartDisplayMode = _
      _manager.SupportedDisplayModes(selectedMode)
    If Not (mode Is Nothing) Then
      _manager.DisplayMode = mode
    End If

  End Sub
   
  ' Set the selected item equal to the current display mode.
  Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Dim items As ListItemCollection = DisplayModeDropdown.Items
    Dim selectedIndex As Integer = _
      items.IndexOf(items.FindByText(_manager.DisplayMode.Name))
    DisplayModeDropdown.SelectedIndex = selectedIndex

  End Sub

  ' Reset all of a user's personalization data for the page.
  Protected Sub LinkButton1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    _manager.Personalization.ResetPersonalizationState()
    
  End Sub

  ' If not in User personalization scope, toggle into it.
  Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    If _manager.Personalization.Scope = PersonalizationScope.Shared Then
      _manager.Personalization.ToggleScope()
    End If

  End Sub
   
  ' If not in Shared scope, and if user is allowed, toggle the scope.
  Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    If _manager.Personalization.CanEnterSharedScope AndAlso _
      _manager.Personalization.Scope = PersonalizationScope.User Then
      _manager.Personalization.ToggleScope()
    End If

  End Sub

</script>
<div>
  <asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="230" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text=" Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" 
      AssociatedControlID="DisplayModeDropdown"/>
    <asp:DropDownList ID="DisplayModeDropdown" runat="server"  
      AutoPostBack="true" 
      Width="120"
      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
    <asp:LinkButton ID="LinkButton1" runat="server"
      Text="Reset User State" 
      ToolTip="Reset the current user's personalization data for the page."
      Font-Size="8" 
      OnClick="LinkButton1_Click" />
    <asp:Panel ID="Panel2" runat="server" 
      GroupingText="Personalization Scope"
      Font-Bold="true"
      Font-Size="8" 
      Visible="false" >
      <asp:RadioButton ID="RadioButton1" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
      <asp:RadioButton ID="RadioButton2" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        OnCheckedChanged="RadioButton2_CheckedChanged" />
    </asp:Panel>
  </asp:Panel>
</div>

程式碼範例的第二個部分是介面和控制項的原始程式碼。 原始程式檔包含名為 的 IZipCode 簡單介面。 另外還有名為 WebPartZipCodeWebPart 的類別,可實作 介面,並做為提供者控制項。 其 ProvideIZipCode 方法是實作介面唯一成員的回呼方法。 方法只會傳回 介面的實例。 請注意,方法會以其 ConnectionProvider 中繼資料中的屬性標示。 這是將方法識別為提供者連接點回呼方法的機制。 另一個 WebPart 類別命名為 WeatherWebPart ,而且它會做為連線的取用者。 這個類別具有名為 GetZipCode 的方法,可從提供者控制項取得介面的 IZipCode 實例。 請注意,這個方法會標示為取用者的連接點方法,其 ConnectionConsumer 中繼資料中有 屬性。

若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 Bin 資料夾或全域組件快取中。 或者,您可以將原始程式碼放在月臺的App_Code資料夾中,在執行時間將動態編譯。 此程式碼範例使用動態編譯。 如需示範如何編譯的逐步解說,請參閱逐步解說 :開發和使用自訂 Web 服務器控制項

namespace Samples.AspNet.CS.Controls
{
  using System;
  using System.Web;
  using System.Web.Security;
  using System.Security.Permissions;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public interface IZipCode
  {
    string ZipCode { get; set;}
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class ZipCodeWebPart : WebPart, IZipCode
  {
    string zipCodeText = String.Empty;
    TextBox input;
    Button send;

    public ZipCodeWebPart()
    {
    }

    // Make the implemented property personalizable to save 
    // the Zip Code between browser sessions.
    [Personalizable()]
    public virtual string ZipCode
    {
      get { return zipCodeText; }
      set { zipCodeText = value; }
    }

    // This is the callback method that returns the provider.
    [ConnectionProvider("Zip Code Provider", "ZipCodeProvider")]
    public IZipCode ProvideIZipCode()
    {
      return this;
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      input = new TextBox();
      this.Controls.Add(input);
      send = new Button();
      send.Text = "Enter 5-digit Zip Code";
      send.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(send);
    }

    private void submit_Click(object sender, EventArgs e)
    {
      if (!string.IsNullOrEmpty(input.Text))
      {
        zipCodeText = Page.Server.HtmlEncode(input.Text);
        input.Text = String.Empty;
      }
    }
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class WeatherWebPart : WebPart
  {
    private IZipCode _provider;
    string _zipSearch;
    Label DisplayContent;

    // This method is identified by the ConnectionConsumer 
    // attribute, and is the mechanism for connecting with 
    // the provider. 
    [ConnectionConsumer("Zip Code Consumer", "ZipCodeConsumer")]
    public void GetIZipCode(IZipCode Provider)
    {
      _provider = Provider;
    }
    
    protected override void OnPreRender(EventArgs e)
    {
      EnsureChildControls();

      if (this._provider != null)
      {
        _zipSearch = _provider.ZipCode.Trim();
        DisplayContent.Text = "My Zip Code is:  " + _zipSearch;
      }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      this.Controls.Add(DisplayContent);
    }
  }
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Interface IZipCode

    Property ZipCode() As String

  End Interface

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class ZipCodeWebPart
    Inherits WebPart
    Implements IZipCode
    Private zipCodeText As String = String.Empty
    Private input As TextBox
    Private send As Button

    Public Sub New()
    End Sub

    ' Make the implemented property personalizable to save 
    ' the Zip Code between browser sessions.
    <Personalizable()> _
    Public Property ZipCode() As String _
      Implements IZipCode.ZipCode

      Get
        Return zipCodeText
      End Get
      Set(ByVal value As String)
        zipCodeText = value
      End Set
    End Property

    ' This is the callback method that returns the provider.
    <ConnectionProvider("Zip Code Provider", "ZipCodeProvider")> _
    Public Function ProvideIZipCode() As IZipCode
      Return Me
    End Function


    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      input = New TextBox()
      Me.Controls.Add(input)
      send = New Button()
      send.Text = "Enter 5-digit Zip Code"
      AddHandler send.Click, AddressOf Me.submit_Click
      Me.Controls.Add(send)

    End Sub


    Private Sub submit_Click(ByVal sender As Object, _
      ByVal e As EventArgs)

      If input.Text <> String.Empty Then
        zipCodeText = Page.Server.HtmlEncode(input.Text)
        input.Text = String.Empty
      End If

    End Sub

  End Class

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class WeatherWebPart
    Inherits WebPart
    Private _provider As IZipCode
    Private _zipSearch As String
    Private DisplayContent As Label

    ' This method is identified by the ConnectionConsumer 
    ' attribute, and is the mechanism for connecting with 
    ' the provider. 
    <ConnectionConsumer("Zip Code Consumer", "ZipCodeConsumer")> _
    Public Sub GetIZipCode(ByVal Provider As IZipCode)
      _provider = Provider
    End Sub


    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
      EnsureChildControls()

      If Not (Me._provider Is Nothing) Then
        _zipSearch = _provider.ZipCode.Trim()
                DisplayContent.Text = "My Zip Code is:  " + _zipSearch
      End If

    End Sub

    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      DisplayContent = New Label()
      Me.Controls.Add(DisplayContent)

    End Sub

  End Class

End Namespace

程式碼範例的第三個部分是網頁。 靠近頂端是 Register 使用者控制項和自訂 WebPart 控制項的指示詞。 因為範例假設控制項的動態編譯,所以控制項的原始程式碼應該位於App_Code子資料夾中; Register 頁面中的標籤只會參考任意標記前置詞和控制項的命名空間。 (提供者和取用者) 的自訂 WebPart 控制項會在網頁的 <asp:webpartzone> 元素內 <zonetemplate> 宣告。

頁面提供三種方式來形成自訂控制項之間的連線。 第一個方法是宣告式方法。 在頁面的標記中, <StaticConnections> 會宣告元素,而該元素是 <asp:WebPartConnections> 元素,其中包含指定為屬性之連接的各種取用者和提供者詳細資料。 這是建立連線的方法之一,方法是直接在網頁中宣告它,特別是元素內 <asp:WebPartManager> 。 由於此靜態連線,所以第一次載入頁面時,會立即建立兩個自訂控制項之間的連線。

在控制項之間建立連接的第二種方法是由頁面中的 元素所 <asp:connectionszone> 提供。 如果使用者在執行時間將頁面切換成連線顯示模式,然後按一下其中一個自訂控制項上的連接動詞,元素 <asp:connectionszone> 會自動轉譯 UI 以建立連接。

此頁面也會示範建立連線的第三種方式,也就是以程式設計方式執行。 在 Button1_Click 方法中,程式碼會 ProviderConnectionPoint 建立提供者控制項的 物件,並藉由呼叫 GetProviderConnectionPoints 方法來擷取其連接點詳細資料。 它會針對取用者控制項執行類似的工作,呼叫 GetConsumerConnectionPoints 方法。 最後,它會在 控制項上 WebPartManager 呼叫 ConnectWebParts 方法,以建立新的 WebPartConnection 物件。

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuCS"
    Src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" %>

<!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 Button1_Click(object sender, EventArgs e)
  {
    ProviderConnectionPoint provPoint = 
      mgr.GetProviderConnectionPoints(zip1)["ZipCodeProvider"];
    ConsumerConnectionPoint connPoint = 
      mgr.GetConsumerConnectionPoints(weather1)["ZipCodeConsumer"];
    WebPartConnection conn1 = mgr.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);
  }

  protected void mgr_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Button1.Visible = true;
    else
      Button1.Visible = false;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
        <StaticConnections>
          <asp:WebPartConnection ID="conn1"
            ConsumerConnectionPointID="ZipCodeConsumer"
            ConsumerID="weather1"
            ProviderConnectionPointID="ZipCodeProvider"
            ProviderID="zip1" />
        </StaticConnections>
      </asp:WebPartManager>
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuVB"
    Src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" %>

<!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 Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim provPoint As ProviderConnectionPoint = _
      mgr.GetProviderConnectionPoints(zip1)("ZipCodeProvider")
    Dim connPoint As ConsumerConnectionPoint = _
      mgr.GetConsumerConnectionPoints(weather1)("ZipCodeConsumer")
    mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)

  End Sub

  Protected Sub mgr_DisplayModeChanged (ByVal sender as Object, _
    ByVal e as WebPartDisplayModeEventArgs)

    If mgr.DisplayMode Is WebPartManager.ConnectDisplayMode Then
    Button1.Visible = True
    Else
    Button1.Visible = False
    End If

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
        <StaticConnections>
          <asp:WebPartConnection ID="conn1"
            ConsumerConnectionPointID="ZipCodeConsumer"
            ConsumerID="weather1"
            ProviderConnectionPointID="ZipCodeProvider"
            ProviderID="zip1" />
        </StaticConnections>
      </asp:WebPartManager>
      <uc1:DisplayModeMenuVB ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </div>
    </form>
</body>
</html>

在瀏覽器中載入網頁之後,第一個連線已經存在,因為它是在 元素內 <StaticConnections> 宣告的。 在 [郵遞區號提供者 ] 控制項中輸入一些文字,並將它顯示在取用者控制項中。 接下來,中斷兩個控制項的連線。 使用 [顯示模式 ] 下拉式清單控制項,將頁面變更為連接顯示模式。 按一下標題列中) 每個控制項的向下箭號所表示的動詞功能表 (WebPart ,並注意每個控制項都有 Connect 選項。 這是連接動詞,只有在頁面處於連接模式時,才會出現在動詞功能表中。 按一下其中一個控制項上的連接動詞,控制項所提供的 ConnectionsZone 連接 UI 隨即出現。 按一下 [ 中斷連線 ] 按鈕,結束控制項之間的靜態連線。 使用 [顯示模式 ] 控制項,將頁面傳回瀏覽模式。 再次嘗試在提供者中輸入一些新的文字,並請注意,因為控制項已中斷連線,所以無法在取用者控制項中更新文字。

接下來,使用與上述相同的方法,再次將頁面切換為連線顯示模式。 按一下其中一個控制項上的連接動詞。 按一下 [ 建立連線 ] 連結,並使用 控制項所提供的 ConnectionsZone UI 來建立控制項之間的連線。 這是建立連線的第二種方法。 請注意,一旦形成連接,您在提供者控制項中輸入的最後一個字串 (因為控制項已中斷連線,) 突然出現在取用者中,因為連線已重新建立,所以無法出現。 按一下 [ 中斷連線 ] 按鈕,結束您剛才建立的目前連線。 傳回頁面以瀏覽模式。 在提供者中輸入一些新文字,以示範文字未更新,且控制項再次中斷連線。

傳回頁面以連接顯示模式。 不要按一下連接動詞,而是按一下 [連接 WebPart 控制項 ] 按鈕,這說明形成連線的第三種方法。 此方法會在一個簡單的步驟中以程式設計方式連接控制項,而不需要使用 ConnectionsZone 控制項。 請注意,建立連接時,您在提供者中輸入的最後一個字串突然出現在取用者控制項中。

備註

在 Web 元件控制項集中,連線是兩 WebPart 個 (或其他伺服器或使用者) 控制項之間的連結或關聯,可讓他們共用資料。 共用資料的能力可讓連線的控制項以超出隔離控制項所提供的功能的方式使用。 例如,如果一個控制項提供郵遞區號資料,而另一個控制項可以讀取該資料,並根據郵遞區號提供當地天氣資訊,則兩個控制項的連線功能會為使用者提供更多價值。 若要擴充此範例,可以建立其他控制項,也會根據郵遞區號顯示資訊,例如具有本機新聞連結的控制項,而且這些控制項都可以與提供郵遞區號的單一控制項共用資料。 網頁元件應用程式的終端使用者可以直接從網頁瀏覽器建立和管理所有這類相容控制項之間的連線、使用標準連線使用者介面 (UI) 由控制項提供 ConnectionsZone ,或使用開發人員提供的自訂 UI。

這個 WebPartConnection 類別概觀是建立連線之基本詳細資料的一般語句。 如需有關建立連線的特定元件和需求的詳細資訊,請參閱 Web 元件連線概觀,或參閱下列討論中所述的參考類別和程式碼範例。 Web 元件連線有數個基本層面:

  • WebPart 個控制項。 每個網頁元件連線都包含兩個控制項。 控制項可以同時參與多個連線,但每個單一連接都只包含兩個控制項。 控制項可以直接衍生自 WebPart 基類,也可以是其他伺服器控制項,包括 ASP.NET 控制項、自訂伺服器控制項和使用者控制項。 如果放在 WebPartZoneBase 區域中,則不會衍生自 類別的 WebPart 控制項會在執行時間自動包裝物件 GenericWebPart ,讓它們繼承自 WebPart 類別,並以執行時間 WebPart 控制項的形式運作。

  • 位於區域中的 WebPartZoneBase 控制項。 WebPart控制項和任何其他類型的伺服器控制項都必須位於區域中 WebPartZoneBase ,才能參與網頁元件連線 (和其他大部分網頁元件功能) 。

  • 取用者和提供者。 每個網頁元件連線都有兩個控制項:資料提供者和資料取用者。 提供者會透過以介面形式傳回資料的指定回呼方法,向取用者提供資料。 (如需如何建立及指定回呼方法的範例,請參閱本主題的範例一節。) 這個回呼方法稱為提供者連接點。 此連接點的詳細資料 (其「易記」名稱、識別碼,以及傳回介面類別型) 包含在與提供者控制項相關聯的物件中 ProviderConnectionPoint 。 取用者會透過可接受 介面實例的指定方法接收資料。 這個方法稱為取用者連接點,而連接點的詳細資料 (名稱、識別碼和介面類別型) 包含在與取用者控制項相關聯的 物件中 ConsumerConnectionPoint

  • 相容的控制項或有效的轉換器。 若要讓連接能夠運作,取用者和提供者必須相容 (表示其指定的連接點方法可以使用相同類型的介面) ,或者必須有 WebPartTransformer 物件能夠將提供者所提供的類型轉譯為取用者所瞭解的類型。

  • WebPartConnection 物件。 若要讓連線存在,必須有 類別的 WebPartConnection 實例,其中包含提供者和取用者控制項的參考,以及其連接點的詳細資料。 如果提供者和取用者不相容,而是使用 WebPartTransformer 物件來連接,則連接會參考轉換器。

  • 建立連線的方法。 使用連接點方法正確設計相容取用者和提供者控制項並放置在區域中,而且 WebPartConnection 可以使用 物件之後,需要的最後一個基本步驟是起始連接。 其中一種方式是讓使用者透過 UI 建立連線。 如果您在頁面上放置元素 <asp:connectionszone> ,而連線的其他必要元件已就緒,則使用者可在執行時間將頁面切換為連線顯示模式,請在提供者或取用者的動詞功能表上按一下連接動詞,並根據控制項) 出現連線 UI ConnectionsZone (。 透過此 UI,使用者可以起始連線。 起始連線的另一種方式是以程式設計方式執行。 不論是透過 UI 還是以程式設計方式,起始連線的基礎方法都相同。 應用程式 ConnectWebParts 會在控制項上使用 WebPartManager 轉換器) ConnectWebParts 、傳遞至提供者、取用者及其各自的連接點物件,以及方法傳回 WebPartConnection 物件時,呼叫 方法 (或 方法。

類別 WebPartConnection 會定義 物件,該物件會封裝兩 WebPart 個控制項之間連接的基本詳細資料。 類別包含幾乎與特定連接詳細資料相關的屬性。 數個屬性牽涉到連接中的取用者控制項。 屬性會 Consumer 參考取用者控制項本身,而 ConsumerID 屬性會參考取用者的識別碼。 ConsumerConnectionPoint物件包含取用者連接點的詳細資料,是由取用者的 ConsumerConnectionPoint 屬性所參考。 屬性 ConsumerConnectionPointID 會參考 物件的識別碼 ConsumerConnectionPoint 。 所有這些取用者相關的連接屬性都必須有指派給它們的值,才能建立連接。

類別 WebPartConnection 也有數個與連接中提供者控制項相關的屬性,而這些屬性會對應至取用者的屬性。 屬性會 Provider 參考提供者控制項本身,而 ProviderID 屬性則參考其識別碼。 屬性 ProviderConnectionPoint 會參考 ProviderConnectionPoint 物件,而 ProviderConnectionPointID 屬性會參考提供者連接點的識別碼。

數個屬性牽涉到連線的狀態。 屬性 IsActive 會指出連線在目前交換資料) 或非作用中 (仍連線,但未主動共用資料) (作用中。 屬性 IsShared 會指出連線是否為共用 (可供頁面) 或使用者特定連線的所有使用者使用,而 IsStatic 屬性會指出控制項是否為靜態 (宣告在頁面標記中,因此以程式設計方式建立的永久) 或動態 (,這表示可以刪除) 。

建構函式

WebPartConnection()

初始化 WebPartConnection 類別的新執行個體。

屬性

Consumer

取得做為連接之中消費者控制項的 WebPart 物件。

ConsumerConnectionPoint

取得做為連接之中消費者控制項連接點的物件。

ConsumerConnectionPointID

取得或設定連接上的屬性值,這個屬性會參考做為此連接之消費者連接點的物件 ID。

ConsumerID

取得或設定連接上的屬性值,這個屬性會參考做為此連接之消費者 WebPart 控制項的 ID。

ID

取得或設定 WebPartConnection 物件的 ID。

IsActive

取得指出目前是否已建立 WebPartConnection 物件,並且其提供者和消費者控制項之間是否可以交換資料的值。

IsShared

取得值,指出 WebPartConnection 物件對所有使用者或只對目前使用者為可見。

IsStatic

取得值,指出 WebPartConnection 物件是在網頁標記中宣告,還是以程式設計方式建立的。

Provider

取得做為 Web 組件連接之中提供者的 WebPart 控制項。

ProviderConnectionPoint

取得做為連接之提供者 WebPart 控制項連接點的物件。

ProviderConnectionPointID

取得或設定連接上的屬性值,這個屬性會參考做為此連接之提供者連接點的物件 ID。

ProviderID

取得或設定連接上的屬性值,這個屬性會參考做為此連接之提供者 WebPart 控制項的 ID。

Transformer

取得 WebPartTransformer 物件,這個物件是用來轉換 Web 組件連接之中兩個不相容連接點的資料。

Transformers

取得 Web 組件控制集內部使用的 WebPartTransformer 物件集合。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

覆寫繼承的 ToString() 方法,並傳回連接物件的簡短型別名稱。

適用於

另請參閱