WebPartConnection クラス

定義

2 つの 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
属性

次のコード例は、2 つの WebPart コントロール間の単純な接続を作成する方法を示しています。 この例では、接続を形成する 3 つの方法を示します。宣言によって、Web ページのマークアップに接続のタグを配置します。コードで接続を作成することによって、プログラムによって。と UI を介して、ページにコントロールを ConnectionsZone 配置することで、ユーザーが接続を確立できるようにします。

このコード例には、次の 4 つの部分があります。

  • ページ上の Web パーツ表示モードを変更できるユーザー コントロール。

  • 接続のプロバイダーとコンシューマーとして機能するインターフェイスと 2 つの WebPart コントロールのソース コード。

  • すべてのコントロールをホストし、コード例を実行する Web ページ。

  • サンプル ページを実行する方法の説明。

このコード例の最初の部分は、ユーザーが Web ページの表示モードを変更できるようにするユーザー コントロールです。 次のソース コードを .ascx ファイルに保存します。このユーザー コントロールの ディレクティブのRegister属性にSrc割り当てられているファイル名を指定します。このファイル名は、ホスト Web ページの上部付近にあります。 表示モードの詳細と、このコントロールのソース コードの説明については、「 チュートリアル: Web パーツ ページでの表示モードの変更」を参照してください。

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

コード例の 2 番目の部分は、 インターフェイスとコントロールのソース コードです。 ソース ファイルには、 という名前 IZipCodeの単純なインターフェイスが含まれています。 インターフェイスを実装し、 WebPart プロバイダー コントロールとして機能する という名前 ZipCodeWebPart のクラスもあります。 その ProvideIZipCode メソッドは、インターフェイスの唯一のメンバーを実装するコールバック メソッドです。 メソッドは、 インターフェイスのインスタンスを単に返します。 メソッドは、そのメタデータに ConnectionProvider 属性でマークされていることに注意してください。 これは、プロバイダーの接続ポイントのコールバック メソッドとして メソッドを識別するためのメカニズムです。 もう 1 つの WebPart クラスは という名前 WeatherWebPartで、接続のコンシューマーとして機能します。 このクラスには、 という名前 GetZipCode のメソッドがあり、プロバイダー コントロールからインターフェイスの IZipCode インスタンスを取得します。 このメソッドは、メタデータに 属性を持つ ConnectionConsumer コンシューマーの接続ポイント メソッドとしてマークされていることに注意してください。

コード例を実行するには、このソース コードをコンパイルする必要があります。 明示的にコンパイルし、結果のアセンブリを Web サイトの 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

コード例の 3 番目の部分は Web ページです。 上部の近くに、 Register ユーザー コントロールとカスタム WebPart コントロールのディレクティブがあります。 この例ではコントロールの動的コンパイルを前提としているため、コントロールのソース コードはApp_Codeサブフォルダー内に存在する必要があります。ページ内の タグは Register 、任意のタグ プレフィックスとコントロールの名前空間のみを参照します。 カスタム WebPart コントロール (プロバイダーとコンシューマー) は、Web ページの <asp:webpartzone> 要素内の 要素内で <zonetemplate> 宣言されます。

このページには、カスタム コントロール間の接続を形成する 3 つの方法が用意されています。 最初のメソッドは宣言型です。 ページのマークアップでは、 <StaticConnections> 属性として指定された接続のさまざまなコンシューマーとプロバイダーの詳細を <asp:WebPartConnections> 持つ 要素が宣言され、その中に 要素が宣言されます。 これは、特に 要素内の Web ページで直接宣言することで、接続を作成する <asp:WebPartManager> 1 つの方法です。 この静的接続のため、2 つのカスタム コントロール間の接続は、ページが初めて読み込まれるとすぐに作成されます。

コントロール間の接続を形成するための 2 つ目のメソッドは、ページの 要素によって <asp:connectionszone> 提供されます。 ユーザーが実行時にページを接続表示モードに切り替え、いずれかのカスタム コントロールで接続動詞をクリックすると、要素は <asp:connectionszone> 接続を作成するための UI を自動的にレンダリングします。

また、このページでは、プログラムによって接続を作成する 3 つ目の方法も示しています。 メソッドでは Button1_Click 、プロバイダー コントロールの オブジェクトを ProviderConnectionPoint 作成し、 メソッドを呼び出して接続ポイントの詳細を GetProviderConnectionPoints 取得します。 コンシューマー コントロールに対して同様のタスクを実行し、 メソッドを GetConsumerConnectionPoints 呼び出します。 最後に、 コントロールで メソッドを呼び出ConnectWebPartsして、新しい WebPartConnection オブジェクトをWebPartManager作成します。

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

ブラウザーで Web ページを読み込んだ後、最初の接続は 要素内で宣言されているため、既に <StaticConnections> 存在します。 郵便番号プロバイダー コントロールにテキストを入力すると、コンシューマー コントロールに表示されます。 次に、2 つのコントロールを切断します。 [表示モード] ドロップダウン リスト コントロールを使用して、ページを接続表示モードに変更します。 各コントロールの動詞メニュー (タイトル バーの下向き矢印で表されます) を WebPart クリックし、それぞれに [接続 ] オプションがあることに注意してください。 これは接続動詞であり、ページが接続モードの場合にのみ動詞メニューに表示されます。 いずれかのコントロールで接続動詞をクリックすると、コントロールによって ConnectionsZone 提供される接続 UI が表示されます。 [ 切断 ] ボタンをクリックして、コントロール間の静的接続を終了します。 表示モード コントロールを使用して、参照モードのページを取得します。 プロバイダーに新しいテキストをもう一度入力してみます。コントロールが切断されているため、コンシューマー コントロールでテキストの更新に失敗します。

次に、上記と同じ方法を使用して、ページを接続表示モードに切り替えます。 いずれかのコントロールで接続動詞をクリックします。 [ 接続の作成 ] リンクをクリックし、コントロールによって提供される UI を ConnectionsZone 使用して、コントロール間の接続を作成します。 これは、接続を作成するための 2 番目の方法です。 接続が作成されるとすぐに、プロバイダー コントロールに最後に入力した文字列 (コントロールが切断されたために表示されませんでした) がコンシューマーに突然表示されます。これは、接続が再作成されたためです。 [ 切断 ] ボタンをクリックして、先ほど作成した現在の接続を終了します。 ページを参照モードに戻します。 プロバイダーに新しいテキストを入力して、テキストが更新されず、コントロールが再び切断されていることを示します。

表示モードを接続するページを返します。 接続動詞をクリックする代わりに、[ WebPart コントロールの接続 ] ボタンをクリックします。これは、接続を形成する 3 番目の方法を示しています。 この方法では、コントロールを使用しなくても、1 つの簡単な手順でプログラムによってコントロールを ConnectionsZone 接続します。 接続が作成されると、プロバイダーに入力した最後の文字列がコンシューマー コントロールに突然表示されることに注意してください。

注釈

Web パーツ コントロール セットでは、接続とは、データを共有できるようにする 2 つの WebPart (または他のサーバーまたはユーザー) コントロール間のリンクまたは関連付けです。 データを共有するこの機能により、分離されたコントロールによって提供される機能を超える方法で、接続されたコントロールを使用できます。 たとえば、あるコントロールが郵便番号データを提供し、別のコントロールがそのデータを読み取り、郵便番号に基づいてローカル気象情報を提供できる場合、2 つのコントロールの接続機能により、ユーザーにより多くの価値が提供されます。 この例を拡張するために、ローカル ニュースへのリンクを含むコントロールなど、郵便番号に基づいて情報を表示する他のコントロールを作成できます。また、郵便番号データを操作できるこれらのコントロールはすべて、郵便番号を提供する単一のコントロールとデータを共有できます。 Web パーツ アプリケーションのエンド ユーザーは、Web ブラウザーから直接、コントロールによって提供される標準接続ユーザー インターフェイス (UI) を使用するか、開発者が提供 ConnectionsZone するカスタム UI を使用して、このような互換性のあるすべてのコントロール間の接続を作成および管理できます。

この WebPartConnection クラスの概要は、接続を作成するための基本的な詳細の一般的なステートメントです。 接続の作成に関連する特定のコンポーネントと要件の詳細については、「 Web パーツ接続の概要」を参照するか、次の説明で説明されている参照クラスとコード例を参照してください。 Web パーツ接続には、いくつかの基本的な側面があります。

  • 2 つの WebPart コントロール。 すべての Web パーツ接続は、2 つのコントロールで構成されます。 1 つのコントロールが複数の接続に同時に参加できますが、すべての接続は 2 つのコントロールで構成されます。 コントロールは基底クラスから WebPart 直接派生することも、ASP.NET コントロール、カスタム サーバー コントロール、ユーザー コントロールなど、他のサーバー コントロールにすることもできます。 クラスからWebPart派生しないコントロールは、ゾーンにWebPartZoneBase配置されている場合、実行時に オブジェクトでGenericWebPart自動的にラップされ、実行時コントロールとしてWebPartクラスと関数からWebPart継承できるようになります。

  • ゾーンに WebPartZoneBase 存在するコントロール。 Web パーツ接続 (およびその他のほとんどの Web パーツ機能) に参加できるようにするには、コントロールと他の種類のサーバー コントロールの両方 WebPart がゾーン内に存在 WebPartZoneBase する必要があります。

  • コンシューマーとプロバイダー。 すべての Web パーツ接続には、データのプロバイダーとデータのコンシューマーという 2 つのコントロールがあります。 プロバイダーは、インターフェイスの形式でデータを返す指定されたコールバック メソッドを使用して、コンシューマーにデータを提供します。 (コールバック メソッドを作成して指定する方法の例については、このトピックの「例」セクションを参照してください)。このコールバック メソッドは、プロバイダー接続ポイントと呼ばれます。 この接続ポイントの詳細 ("フレンドリ" 名、ID、および返されるインターフェイスの型) は、プロバイダー コントロールに関連付けられているオブジェクトに ProviderConnectionPoint 含まれています。 コンシューマーは、インターフェイスのインスタンスを受け入れることができる指定されたメソッドを介してデータを受信します。 このメソッドはコンシューマー接続ポイントと呼ばれ、接続ポイントの詳細 (名前、ID、インターフェイスの種類) は、コンシューマー コントロールに関連付けられているオブジェクトに ConsumerConnectionPoint 含まれています。

  • 互換性のあるコントロールまたは有効なトランスフォーマー。 接続を機能させるには、コンシューマーとプロバイダーが互換性を持っているか (指定された接続ポイント メソッドが同じ種類のインターフェイスで動作することを意味します)、またはプロバイダーによって提供される型をコンシューマーが認識する型に変換できるオブジェクトが存在する必要があります WebPartTransformer

  • WebPartConnection オブジェクト。 接続を存在させるには、プロバイダーコントロールとコンシューマー コントロールへの参照と、その接続ポイントの詳細を含む クラスのインスタンス WebPartConnection が存在する必要があります。 プロバイダーとコンシューマーに互換性がない場合、代わりに オブジェクトを WebPartTransformer 使用して接続すると、接続はトランスフォーマーを参照します。

  • 接続を確立する手段。 互換性のあるコンシューマーコントロールとプロバイダーコントロールが接続ポイントメソッドで適切に設計され、ゾーンに配置され WebPartConnection 、オブジェクトが使用可能な場合、最後に必要な基本的な手順は接続を開始することです。 これを行う方法の 1 つは、ユーザーが UI を介して接続を作成することです。 ページに要素を <asp:connectionszone> 配置し、接続に必要なその他のコンポーネントが配置されている場合、実行時にユーザーはページを接続表示モードに切り替えることができます。プロバイダーまたはコンシューマーのいずれかの動詞メニューで connect 動詞をクリックすると、接続 UI (コントロールに ConnectionsZone 基づく) が表示されます。 この UI を使用して、ユーザーは接続を開始できます。 接続を開始するもう 1 つの方法は、プログラムで行うことです。 どちらの場合も、UI を使用する場合もプログラムを使用する場合でも、接続を開始する基になるメソッドは同じです。 アプリケーションはコントロールの ConnectWebParts メソッド (または ConnectWebParts トランスフォーマーを使用している場合は メソッド) WebPartManager を呼び出し、プロバイダー、コンシューマー、およびそれぞれの接続ポイント オブジェクトを渡し、メソッドは オブジェクトを WebPartConnection 返します。

クラスは WebPartConnection 、2 つの WebPart コントロール間の接続の重要な詳細をカプセル化する オブジェクトを定義します。 クラスは、特定の接続の詳細に関連するプロパティでほぼ完全に構成されます。 いくつかのプロパティは、接続でのコンシューマー コントロールに関係します。 プロパティは Consumer コンシューマー コントロール自体を参照し、 プロパティは ConsumerID コンシューマーの ID を参照します。 ConsumerConnectionPointコンシューマーの接続ポイントの詳細を含む オブジェクトは、コンシューマーの ConsumerConnectionPoint プロパティによって参照されます。 プロパティは ConsumerConnectionPointID 、 オブジェクトの ID を ConsumerConnectionPoint 参照します。 これらすべてのコンシューマー関連の接続プロパティには、接続を作成するために値が割り当てられている必要があります。

WebPartConnectionクラスには、接続のプロバイダー コントロールに関連するいくつかのプロパティもあり、これらはコンシューマーのプロパティに対応します。 プロパティは Provider プロバイダー コントロール自体を参照し、プロパティは ProviderID ID を参照します。 プロパティは ProviderConnectionPoint オブジェクトを ProviderConnectionPoint 参照し、 プロパティは ProviderConnectionPointID プロバイダーの接続ポイントの ID を参照します。

いくつかのプロパティは、接続の状態に関係します。 プロパティは IsActive 、接続がアクティブ (現在データを交換中) か非アクティブ (まだ接続されているが、アクティブにデータを共有していない) かを示します。 プロパティは IsShared 、接続が共有 (ページのすべてのユーザーが使用できる) か、ユーザー固有の接続であるかを示し IsStatic 、 プロパティは、コントロールが静的 (ページ マークアップで宣言されているため永続的) か動的 (プログラムによって作成され、削除できることを意味します) を示します。

コンストラクター

WebPartConnection()

WebPartConnection クラスの新しいインスタンスを初期化します。

プロパティ

Consumer

接続でコンシューマー コントロールとして動作する WebPart オブジェクトを取得します。

ConsumerConnectionPoint

接続でコンシューマーとして動作するコントロールに対するコネクション ポイントとしての役割を果たすオブジェクトを取得します。

ConsumerConnectionPointID

接続のコンシューマー接続ポイントとしての役割を果たすオブジェクトの ID を参照する接続のプロパティ値を取得または設定します。

ConsumerID

接続のコンシューマーとして動作する WebPart コントロールの ID を参照する接続のプロパティ値を取得または設定します。

ID

WebPartConnection オブジェクトの ID を取得または設定します。

IsActive

現在 WebPartConnection オブジェクトが確立されていて、そのプロバイダー コントロールとコンシューマー コントロールの間でデータを交換できるかどうかを示す値を取得します。

IsShared

WebPartConnection オブジェクトをすべてのユーザーに表示するかまたは現在のユーザーのみに表示するかを示す値を取得または設定します。

IsStatic

WebPartConnection オブジェクトが Web ページのマークアップに宣言されているか、またはプログラムで作成されるかを示す値を取得します。

Provider

Web パーツ接続でプロバイダーとして動作する WebPart コントロールを取得します。

ProviderConnectionPoint

接続でプロバイダーとして動作する WebPart コントロールに対するコネクション ポイントとしての役割を果たすオブジェクトを取得します。

ProviderConnectionPointID

接続のプロバイダー コネクション ポイントとしての役割を果たすオブジェクトの ID を参照する接続のプロパティ値を取得または設定します。

ProviderID

接続のプロバイダーとして動作する WebPart コントロールの ID を参照する接続のプロパティ値を取得または設定します。

Transformer

Web パーツ接続の 2 つの異なった互換性のないコネクション ポイントの間でデータを変換するために使用する WebPartTransformer オブジェクトを取得します。

Transformers

Web パーツ コントロール セットで内部的に使用する WebPartTransformer オブジェクトのコレクションを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

継承した ToString() メソッドをオーバーライドし、接続オブジェクトの short 型の名前を返します。

適用対象

こちらもご覧ください