ConnectionPoint クラス

定義

Web パーツ接続で、コンシューマー コントロールとプロバイダー コントロールがデータを共有できるようにするコネクション ポイント オブジェクトを定義するための基本クラスとして機能します。

public ref class ConnectionPoint abstract
public abstract class ConnectionPoint
type ConnectionPoint = class
Public MustInherit Class ConnectionPoint
継承
ConnectionPoint
派生

次のコード例は、必要な ConnectionPoint オブジェクトを含む Web パーツ接続の作成を示しています。 ConnectionPointクラスは抽象基底クラスであるため、その子クラスのインスタンスはProviderConnectionPointConsumerConnectionPoint、接続を形成するために使用される実際のオブジェクトです。

この例には、次の 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 接続を形成するカスタム コントロールを登録するためのディレクティブと、ユーザーがページ上の表示モードを変更できるようにするユーザー コントロールがあります。 接続自体は、ページ上の 要素内で <staticconnections> 宣言的に作成されます。 プログラムで接続を作成することもできます。を実行するためのコードは、 メソッドに Button1_Click 含まれています。 接続を宣言的に作成するかプログラムで作成するかに関係なく、プロバイダーとコンシューマーの両方に接続ポイントを常に指定する必要があります。 メソッドは Button2_Click 、プロバイダーとコンシューマーの ConnectionPoint 両方のオブジェクトにアクセスし、そのプロパティ値の一部をページ内のラベルに書き込みます。

<%@ 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"];
      
    if(mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint))
      mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint);
  
  }  
  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartConnection conn = mgr.Connections[0];
    
    lblConn.Text = "<h2>Connection Point Details</h2>" + 
       "<h3>Provider Connection Point</h3>" + 
       "  Display name: " + conn.ProviderConnectionPoint.DisplayName + 
       "<br />" + 
       "  ID: " + conn.ProviderConnectionPoint.ID + 
       "<br />" + 
       "  Interface type: " + 
        conn.ProviderConnectionPoint.InterfaceType.ToString() + 
       "<br />" + 
       "  Control type: " + conn.ProviderConnectionPoint.ControlType.ToString() + 
       "<br />" + 
       "  Allows multiple connections: " + 
          conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() + 
       "<br />" + 
       "  Enabled: " + conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() + 
       "<hr />" + 
       "<h3>Consumer Connection Point</h3>" + 
       "  Display name: " + conn.ConsumerConnectionPoint.DisplayName + 
       "<br />" + 
       "  ID: " + conn.ConsumerConnectionPoint.ID + 
       "<br />" + 
       "  Interface type: " + conn.ConsumerConnectionPoint.InterfaceType.ToString() + 
       "<br />" + 
       "  Control type: " + conn.ConsumerConnectionPoint.ControlType.ToString() + 
       "<br />" + 
       "  Allows multiple connections: " + 
          conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() + 
       "<br />" + 
       "  Enabled: " + conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString();
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    lblConn.Text = String.Empty;
  }
</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" >
        <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="Dynamic Connection" 
        OnClick="Button1_Click" />      
      <br />
      <asp:Button ID="Button2" runat="server" 
        Text="Connection Point Details" 
        OnClick="Button2_Click" />
      <br />
      <asp:Label ID="lblConn" 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" %>
    
<!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")

    If mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint) Then
      mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)
    End If
    
  End Sub
  
  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim conn As WebPartConnection = mgr.Connections(0)

    lblConn.Text = "<h2>Connection Point Details</h2>" & _
      "<h3>Provider Connection Point</h3>" & _
      "  Display name: " & conn.ProviderConnectionPoint.DisplayName & _
      "<br />" & _
      "  ID: " & conn.ProviderConnectionPoint.ID & _
      "<br />" & _
      "  Interface type: " & conn.ProviderConnectionPoint.InterfaceType.ToString() & _
      "<br />" & _
      "  Control type: " & conn.ProviderConnectionPoint.ControlType.ToString() & _
      "<br />" & _
      "  Allows multiple connections: " & _
        conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() & _
      "<br />" & _
      "  Enabled: " & conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() & _
      "<hr />" & _
      "<h3>Consumer Connection Point</h3>" & _
      "  Display name: " & conn.ConsumerConnectionPoint.DisplayName & _
      "<br />" & _
      "  ID: " & conn.ConsumerConnectionPoint.ID & _
      "<br />" & _
      "  Interface type: " & conn.ConsumerConnectionPoint.InterfaceType.ToString() & _
      "<br />" & _
      "  Control type: " & conn.ConsumerConnectionPoint.ControlType.ToString() & _
      "<br />" & _
      "  Allows multiple connections: " & _
        conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() & _
      "<br />" & _
      "  Enabled: " & conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString()
          
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    lblConn.Text = String.Empty
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" >
        <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="Dynamic Connection" 
        OnClick="Button1_Click" />      
      <br />
      <asp:Button ID="Button2" runat="server" 
        Text="Connection Point Details" 
        OnClick="Button2_Click" />
      <br />
      <asp:Label ID="lblConn" runat="server" />
    </div>
    </form>
</body>
</html>

ブラウザーでページを読み込んだ後、[ 接続ポイントの詳細 ] ボタンをクリックします。 宣言接続で確立されたプロバイダーとコンシューマー接続ポイントに関する情報が表示されます。 次に、[ 表示モード ] ドロップダウン コントロールを使用して、ページを接続モードに切り替えます。 いずれかのコントロールの WebPart [動詞] メニュー (タイトル バーの下向き矢印で表されます) で、[動詞の接続] をクリックします。 接続 UI が表示され、ページで宣言されたコントロールによって <asp:connectionszone> 自動的に作成されます。 [ 切断 ] ボタンをクリックして、既存の接続を終了します。 表示モード コントロールを使用して、ページを参照モードに戻します。 次に、[ 動的接続 ] ボタンをクリックして、プログラムによって接続を作成します。 もう一度 [ 接続ポイントの詳細 ] ボタンをクリックして、2 つの接続ポイント オブジェクトの詳細を示します。

注釈

すべての Web パーツ接続は、データを共有する 2 つのサーバー コントロールで構成されます。1 つのコントロールはコンシューマー、もう 1 つはプロバイダーです。 Web パーツ接続の重要なコンポーネントと、接続オブジェクト自体の詳細については、クラスの概要に関するページを WebPartConnection 参照してください。 すべての Web パーツ接続には、接続ポイントが必要です。 プロバイダーとコンシューマー コントロールには、コントロールが別のコントロールに接続する方法と共有できるデータの種類の詳細を含む、少なくとも 1 つの定義済 ConnectionPoint みオブジェクト (必要に応じて複数の接続ポイントを持つことができます) が必要です。 実際の接続では、プロバイダーには独自の種類の接続ポイント オブジェクト (基底 ConnectionPoint クラス ProviderConnectionPoint から派生)、インスタンスがあり、コンシューマーには独自のオブジェクトである インスタンスがあります ConsumerConnectionPoint

プロバイダー接続ポイントを作成するには、実装されたインターフェイス インスタンスをコンシューマーに返すコールバック メソッドをプロバイダーに追加する必要があります。 ソース コード内のコールバック メソッドをコード属性でマークする ConnectionProvider 必要があります (クラスを ConnectionProviderAttribute 参照)。 同様に、コンシューマー接続ポイントを作成するには、インターフェイス インスタンスを受け取るメソッドをコンシューマーに追加し、そのメソッドを 属性で ConnectionConsumer マークする必要があります (クラスを ConnectionConsumerAttribute 参照)。

開発者は、Web ページで接続を作成するときに、静的または動的な接続として作成できます。 静的接続は、Web ページのマークアップで宣言されます。 静的接続が宣言されている場合、開発者は、 要素タグ内の 属性と ConsumerConnectionPointID 属性に値を割り当てることで、コンシューマーとプロバイダーの両方にProviderConnectionPointID使用される接続ポイントを<asp:webpartconnection>指定できます。 この方法は、静的接続で接続に使用する接続ポイントを識別できるため、コンシューマーコントロールとプロバイダーコントロール内に複数の接続ポイントが定義されている場合に特に便利です。

動的接続は、開発者のコードによって、またはコントロールによって提供されるユーザー インターフェイス (UI) を介してユーザーによってプログラムによって ConnectionsZone 作成されます。 コードで接続を作成するには、開発者は コントロールの メソッドを WebPartConnection 呼び出して オブジェクトのインスタンスを ConnectWebParts 取得する WebPartManager 必要があります。 このメソッドを呼び出す前に、開発者はコンシューマーおよびプロバイダー サーバー コントロール、およびそれぞれの接続ポイント オブジェクトへの参照を持っている必要があります。 プロバイダーとコンシューマー コントロールの接続ポイントへの参照を取得するために、開発者は最初に コントロールの GetProviderConnectionPoints メソッドと GetConsumerConnectionPoints メソッドを WebPartManager 呼び出します。 これらのメソッドは、適切な接続ポイント オブジェクトを返します。これにより、接続を作成するための メソッドに渡すことができます。

プロバイダーとコンシューマーの接続ポイント オブジェクトの両方が同じ種類のインターフェイスで動作する場合は、互換性があり、データを共有するための直接接続を形成できます。 同じインターフェイス型で動作しない場合は、 オブジェクトを WebPartTransformer 使用して、インターフェイス インスタンスをプロバイダーからコンシューマーが操作できる型に変換する必要があります。

抽象 ConnectionPoint クラスは、コンシューマー コントロールとプロバイダー コントロールの両方で共有される接続ポイントの基本の詳細を提供します。 いくつかのプロパティには、これらの詳細が含まれています。 プロパティは AllowsMultipleConnections 、接続ポイントが一度に複数の接続に参加できるかどうかを示します。 既定では、プロバイダー接続ポイントは複数の接続に参加でき、コンシューマー接続ポイントは参加できません。 プロパティは ControlType 、接続ポイントに関連付けられているサーバー コントロールの種類を示します。 コントロールは接続を形成できるだけでなく WebPart 、ASP.NET コントロール、カスタム コントロール、ユーザー コントロールのいずれであっても、ゾーンに配置 WebPartZoneBase されている場合に接続に参加できるように設定できるサーバー コントロールであることに注意してください。 プロパティは DisplayName 、接続を作成しているユーザーを支援するために UI に表示できる接続ポイントのフレンドリ名を提供します。 プロパティは ID 、接続ポイント オブジェクト自体の文字列識別子として機能します。 プロパティは InterfaceType 、接続ポイントが認識するインターフェイス インスタンスの種類を示します。 特定の接続ポイントがそのインターフェイスのインスタンスを提供または使用するかどうかは、 と オブジェクトのどちらであるかProviderConnectionPointConsumerConnectionPointによって決まります。

クラスには ConnectionPoint 1 つのメソッドがあります。 メソッドは GetEnabled 、接続ポイントが現在接続に参加できるかどうかを示すブール値を返します。

クラスには ConnectionPoint 、 という 1 つのパブリック フィールド DefaultIDもあります。 このフィールドには、接続内の既定の接続ポイントを識別するために使用される値が含まれています。

注意

接続ポイント メソッドを指定するための属性には必須のパラメーターが 1 つしかないため、 displayNameID を指定せずに既定の接続ポイント属性を作成できます。 このような場合、フィールドは DefaultID 使用する基本値を提供します。

フィールド

DefaultID

サーバー コントロールに関連付けられているコネクション ポイントのコレクション内で、既定のコネクション ポイントを識別するために使用される文字列を表します。

プロパティ

AllowsMultipleConnections

コネクション ポイントが同時に複数の接続をサポートするかどうかを示す値を取得します。

ControlType

コネクション ポイントが関連付けられているサーバー コントロールの Type を取得します。

DisplayName

ユーザー インターフェイス (UI) でコネクション ポイントを表す表示名として使用される文字列を取得します。

ID

コネクション ポイントの識別子を含む文字列を取得します。

InterfaceType

コネクション ポイントによって使用されるインターフェイスの型を取得します。

メソッド

Equals(Object)

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

(継承元 Object)
GetEnabled(Control)

コネクション ポイントが接続に参加できるかどうかを示す値を取得します。

GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください