WebPartConnectionとは? わかりやすく解説

WebPartConnection クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

2 つの WebPart コントロールで接続を構成できるようにするオブジェクトを提供します。このクラスは継承できません。

名前空間: System.Web.UI.WebControls.WebParts
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 WebPartConnection
Dim instance As WebPartConnection
public sealed class WebPartConnection
public ref class WebPartConnection sealed
public final class WebPartConnection
public final class WebPartConnection
解説解説

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

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

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

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

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

使用例使用例

2 つの WebPart コントロール間に簡単な接続を作成する方法のコード例を次に示します。この例では、宣言による方法 (Web ページのマークアップに接続のタグを配置する)、プログラムによる方法 (コードで接続を作成する)、および UI を使用する方法 (ページ上に ConnectionsZone コントロールを配置してユーザーが接続を確立できるようにする) の 3 種類の接続構成方法を示します。

コード例は、次の 4 つの部分から成ります。

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

<%@ 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="&nbsp;Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" />
    <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="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="&nbsp;Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" />
    <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 という名前の単純なインターフェイスが含まれています。また、インターフェイスを実装してプロバイダ コントロールとして動作する、ZipCodeWebPart という名前の WebPart クラスも存在します。その ProvideIZipCode メソッドは、インターフェイスの唯一のメンバが実装されたコールバック メソッドです。このメソッドは、単にインターフェイスのインスタンスを返すだけです。このメソッドはメタデータに ConnectionProvider 属性でマークされます。これは、メソッドをプロバイダのコネクション ポイントのコールバック メソッドとして識別するしくみです。もう 1 つの WebPart クラスは WeatherWebPart という名前で、接続のコンシューマとして動作します。このクラスは、プロバイダ コントロールから IZipCode インターフェイスのインスタンスを取得する、GetZipCode という名前のメソッドを持っています。このメソッドは、コンシューマのコネクション ポイント メソッドとして、メタデータに ConnectionConsumer 属性でマークされます。

コード例を実行するためには、このソース コードをコンパイルする必要があります。それを明示的にコンパイルし、コンパイル済みのアセンブリを Web サイトの Bin フォルダまたはグローバル アセンブリ キャッシュに配置できます。サイトの App_Code フォルダにソース コードを配置し、実行時に動的にコンパイルすることもできます。このコード例は、動的コンパイルを使用します。コンパイル方法を示すチュートリアルについては、「チュートリアル : カスタム サーバー コントロールの開発と使用」を参照してください。

Imports System
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 'OnPreRender

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

    End Sub

  End Class

End Namespace
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 (input.Text != String.Empty)
      {
        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);
    }

  }
}

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

このページは、次の 3 つの方法でカスタム コントロール間の接続を構成します。最初の方法では、宣言を使用します。ページのマークアップで、<StaticConnections> 要素を宣言し、その内部に、接続のコンシューマとプロバイダのさまざまな詳細が属性として指定された <asp:WebPartConnections> 要素を宣言します。これは、Web ページに直接宣言することにより接続を作成する方法の 1 つで、具体的には <asp:WebPartManager> 要素内に宣言されています。これは静的接続であるため、2 つのカスタム コントロール間の接続は、最初にページを読み込んだ直後に作成されます。

コントロール間の接続を構成する 2 番目の方法では、ページに <asp:connectionszone> 要素を指定します。実行時にユーザーがページを接続表示モードに切り替え、1 つのカスタム コントロールの Connect 動詞をクリックすると、接続を作成する UI が <asp:connectionszone> 要素によって自動的に表示されます。

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

<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuVB"
    Src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls"
 %>

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

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

ブラウザに Web ページを読み込むと、最初の接続は既に存在しています。これはこの接続が <StaticConnections> 要素内に宣言されているためです。[ZIP Code Provider] コントロールに何らかのテキストを入力すると、それがコンシューマ コントロールに表示されます。次に、2 つのコントロールの接続を解除します。[Display Mode] ドロップダウン リスト コントロールを使用して、ページを接続表示モードに変更します。それぞれの WebPart コントロールで、タイトル バーに下向きの矢印で表示された動詞メニューをクリックし、それぞれに [接続] オプションがあることを確認します。これは Connect 動詞で、ページが接続モードにある場合にのみ動詞メニューに表示されます。1 つのコントロールの Connect 動詞をクリックすると、ConnectionsZone コントロールに用意されている接続 UI が表示されます。[切断] をクリックし、コントロール間の静的接続を終了します。[Display Mode] コントロールを使用して、ページをブラウズ モードに戻します。プロバイダでもう一度何らかの新しいテキストを入力します。コントロールの接続が解除されているため、コンシューマ コントロールのテキストが更新されないことを確認します。

次に、上と同じ方法で、もう一度ページを接続表示モードに切り替えます。1 つのコントロールの Connect 動詞をクリックします。[Create a Connection] リンクをクリックし、ConnectionsZone コントロールに用意されている UI を使用して、コントロール間の接続を作成します。これが、接続を作成する 2 番目の方法です。接続の構成直後、プロバイダ コントロールに前回入力したが、コントロールの接続が解除されていたため表示されなかった文字列が、コンシューマに突然表示されます。これは、接続が再作成されたためです。[切断] をクリックし、今作成した現在の接続を終了します。ページをブラウズ モードに戻します。プロバイダに何らかの新しいテキストを入力し、テキストが更新されないことを確認します。これは、コントロールの接続が再び解除されたためです。

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

.NET Framework のセキュリティ.NET Framework のセキュリティ
継承階層継承階層
System.Object
  System.Web.UI.WebControls.WebParts.WebPartConnection
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
WebPartConnection メンバ
System.Web.UI.WebControls.WebParts 名前空間
ConnectionPoint クラス
ProviderConnectionPoint クラス
ConsumerConnectionPoint クラス
WebPartManager
ConnectionsZone クラス
WebPartTransformer
ConnectWebParts
その他の技術情報
Web パーツ接続の概要
ASP.NET Web パーツ ページ

WebPartConnection コンストラクタ

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Web.UI.WebControls.WebParts
アセンブリ: System.Web (system.web.dll 内)
構文構文

Dim instance As New WebPartConnection
public WebPartConnection ()
public:
WebPartConnection ()
public WebPartConnection ()
public function WebPartConnection ()
解説解説

WebPartConnection コンストラクタはパブリックであるため、Web ページのマークアップで接続を宣言すると ASP.NET パーサーがインスタンスを作成できます。このコンストラクタは開発者のコードでの使用は想定していません。

プログラムで WebPartConnection オブジェクトを作成する場合は、WebPartManager コントロールに用意されている WebPartManager.ConnectWebParts メソッドまたは WebPartManager.ConnectWebParts メソッドを使用する必要があります。これらのメソッドは、接続の作成における多くの詳細要件を処理し、WebPartConnection オブジェクトを返します。

WebPartConnection クラスの WebPartConnection コンストラクタは、IsShared プロパティおよび IsStatic プロパティの値を true に設定します。これにより、新しい接続の既定の状態は静的になり (つまり、含まれる Web ページのマークアップで宣言され)、共有されます (ページのすべてのユーザーに表示されます)。これが、<asp:webpartconnection> 要素でページのマークアップに接続を宣言して接続を追加する場合の一般的な手順です。ただし、別の環境では、接続はこれらのプロパティに対して異なる値を持ちます。たとえば、コードで接続オブジェクトを作成してプログラムで接続を追加し、ユーザーが既定のユーザー パーソナル化スコープでページを表示する場合、接続は、動的なユーザー別 (特定のユーザーに対してのみ表示) の接続として作成されます。

メモメモ

IsShared プロパティおよび IsStatic プロパティは読み取り専用です。また、接続の性質を決定するこれらのプロパティは、接続の作成方法 (ページ マークアップによる宣言、独自のコードを使用したプログラム、またはユーザー インターフェイス) に応じて WebPartManager コントロールによって内部的に設定されます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
WebPartConnection クラス
WebPartConnection メンバ
System.Web.UI.WebControls.WebParts 名前空間
ConnectWebParts
その他の技術情報
ASP.NET Web パーツ ページ

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 オブジェクトのコレクションを取得します。
参照参照

関連項目

WebPartConnection クラス
System.Web.UI.WebControls.WebParts 名前空間
ConnectionPoint クラス
ProviderConnectionPoint クラス
ConsumerConnectionPoint クラス
WebPartManager
ConnectionsZone クラス
WebPartTransformer
ConnectWebParts

その他の技術情報

Web パーツ接続の概要
ASP.NET Web パーツ ページ

WebPartConnection メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

WebPartConnection クラス
System.Web.UI.WebControls.WebParts 名前空間
ConnectionPoint クラス
ProviderConnectionPoint クラス
ConsumerConnectionPoint クラス
WebPartManager
ConnectionsZone クラス
WebPartTransformer
ConnectWebParts

その他の技術情報

Web パーツ接続の概要
ASP.NET Web パーツ ページ

WebPartConnection メンバ

2 つの WebPart コントロールで接続を構成できるようにするオブジェクトを提供します。このクラスは継承できません。

WebPartConnection データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド 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 オブジェクトのコレクションを取得します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

WebPartConnection クラス
System.Web.UI.WebControls.WebParts 名前空間
ConnectionPoint クラス
ProviderConnectionPoint クラス
ConsumerConnectionPoint クラス
WebPartManager
ConnectionsZone クラス
WebPartTransformer
ConnectWebParts

その他の技術情報

Web パーツ接続の概要
ASP.NET Web パーツ ページ



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「WebPartConnection」の関連用語

WebPartConnectionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



WebPartConnectionのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS