ConsumerConnectionPoint コンストラクター

定義

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

public:
 ConsumerConnectionPoint(System::Reflection::MethodInfo ^ callbackMethod, Type ^ interfaceType, Type ^ controlType, System::String ^ displayName, System::String ^ id, bool allowsMultipleConnections);
public ConsumerConnectionPoint(System.Reflection.MethodInfo callbackMethod, Type interfaceType, Type controlType, string displayName, string id, bool allowsMultipleConnections);
new System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint : System.Reflection.MethodInfo * Type * Type * string * string * bool -> System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint
Public Sub New (callbackMethod As MethodInfo, interfaceType As Type, controlType As Type, displayName As String, id As String, allowsMultipleConnections As Boolean)

パラメーター

callbackMethod
MethodInfo

接続を確立するためにコンシューマーにインターフェイス インスタンスを返すコンシューマー コントロール内のメソッド。

interfaceType
Type

コンシューマーがプロバイダーから受け取るインターフェイスの Type

controlType
Type

コンシューマー接続ポイントが関連付けられているコンシューマー コントロールの Type

displayName
String

接続ユーザー インターフェイス (UI) でユーザーに表示されるコンシューマー接続ポイントのフレンドリ表示名。

id
String

コンシューマー接続ポイントの一意識別子。

allowsMultipleConnections
Boolean

コンシューマー接続ポイントがプロバイダーとの複数の同時接続を持つことができるかどうかを示すブール値。

例外

callbackMethodnullです。

-又は-

interfaceTypenullです。

-又は-

controlTypenullです。

-又は-

displayNamenull または空の文字列 ("") です。

controlType がコンシューマー コントロール (またはコンシューマー コントロールから派生した有効なクラス) と同じ型ではありません。

次のコード例では、 ConsumerConnectionPoint クラスから派生してカスタム プロバイダー接続ポイントを作成する方法を示します。

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

  • プロバイダー WebPart コントロール、コンシューマー WebPart コントロール、およびカスタム ConsumerConnectionPoint オブジェクトを含むソース ファイル。

  • 静的接続でコントロールをホストする Web ページ。

  • コード例を実行する方法の説明。

コード例の最初の部分は、プロバイダーとコンシューマーのWebPart コントロールのソースと、ConsumerConnectionPointという名前のカスタム TableConsumerConnectionPoint クラスです。 TableConsumerConnectionPoint クラスのコンストラクターは基本コンストラクターを呼び出し、「パラメーター」セクションで示されているように必要なパラメーターを渡すことに注意してください。 また、 TableConsumer クラスでは、 SetConnectionInterface メソッドは接続のコールバック メソッドとして指定され、 ConnectionConsumer 属性はカスタム TableConsumerConnectionPoint をパラメーターとして宣言します。 これは、カスタム コンシューマー接続ポイントを作成し、それをコンシューマー コントロールに関連付ける方法を示しています。 この例では、ソース コードが動的にコンパイルされることを前提としているため、Web アプリケーションのApp_Codeサブフォルダーにソース コード ファイルを配置する必要があります。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

//This sample code creates a Web Parts control that acts as a provider of table data.
namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class TableProviderWebPart : WebPart, IWebPartTable
    {
        DataTable _table;

        public TableProviderWebPart()
        {
            _table = new DataTable();

            DataColumn col = new DataColumn();
            col.DataType = typeof(string);
            col.ColumnName = "Name";
            _table.Columns.Add(col);

            col = new DataColumn();
            col.DataType = typeof(string);
            col.ColumnName = "Address";
            _table.Columns.Add(col);

            col = new DataColumn();
            col.DataType = typeof(int);
            col.ColumnName = "ZIP Code";
            _table.Columns.Add(col);

            DataRow row = _table.NewRow();
            row["Name"] = "John Q. Public";
            row["Address"] = "123 Main Street";
            row["ZIP Code"] = 98000;
            _table.Rows.Add(row);
        }

        public PropertyDescriptorCollection Schema
        {
            get
            {
                return TypeDescriptor.GetProperties(_table.DefaultView[0]);
            }
        }
        public void GetTableData(TableCallback callback)
        {
                callback(_table.Rows);
        }

        public bool ConnectionPointEnabled
        {
            get
            {
                object o = ViewState["ConnectionPointEnabled"];
                return (o != null) ? (bool)o : true;
            }
            set
            {
                ViewState["ConnectionPointEnabled"] = value;
            }
        }

        [ConnectionProvider("Table", typeof(TableProviderConnectionPoint), AllowsMultipleConnections = true)]
        public IWebPartTable GetConnectionInterface()
        {
            return new TableProviderWebPart();
        }

        public class TableProviderConnectionPoint : ProviderConnectionPoint
        {
            public TableProviderConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType,
            string name, string id, bool allowsMultipleConnections) : base(
                callbackMethod, interfaceType, controlType,
                name, id, allowsMultipleConnections)
            {
            }
            public override bool GetEnabled(Control control)
            {
                return ((TableProviderWebPart)control).ConnectionPointEnabled;
            }
        }
    }
    
    // This code sample demonstrates a custom WebPart controls that acts as 
    // a consumer in a Web Parts connection.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TableConsumer : WebPart
  {
    private IWebPartTable _provider;
    private ICollection _tableData;

    private void GetTableData(object tableData)
    {
      _tableData = (ICollection)tableData;
    }

    protected override void OnPreRender(EventArgs e)
    {
      if (_provider != null)
      {
        _provider.GetTableData(new TableCallback(GetTableData));
      }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
      if (_provider != null)
      {
        PropertyDescriptorCollection props = _provider.Schema;
        int count = 0;
        if (props != null && props.Count > 0 && _tableData != null)
        {
          foreach (PropertyDescriptor prop in props)
          {
            foreach (DataRow o in _tableData)
            {
              writer.Write(prop.DisplayName + ": " + o[count]);
            }
            writer.WriteBreak();
            writer.WriteLine();
            count = count + 1;
          }
        }
        else
        {
          writer.Write("No data");
        }
      }
      else
      {
        writer.Write("Not connected");
      }
    }
    [ConnectionConsumer("Table")]
    public void SetConnectionInterface(IWebPartTable provider)
    {
      _provider = provider;
    }

    public class TableConsumerConnectionPoint : ConsumerConnectionPoint
    {
      public TableConsumerConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType,
      string name, string id, bool allowsMultipleConnections)
        : base(
        callbackMethod, interfaceType, controlType,
        name, id, allowsMultipleConnections)
      {
      }
    }
  }
}

コード例の 2 番目の部分は、静的な Web パーツ接続でカスタム コントロールをホストする Web ページです。 ページの上部には、カスタム コントロールのプレフィックスと名前空間を宣言する Register ディレクティブがあります。 接続は <asp:webpartconnection> 要素を使用して宣言され、プロバイダーコントロールとコンシューマー コントロールは <asp:webpartzone> 要素内で宣言されます。

<%@ page language="C#" %>
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>IField Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:webpartmanager id="WebPartManager1" runat="server">
            <StaticConnections>
                <asp:WebPartConnection id="wp1" ProviderID="provider1" ConsumerID="consumer1">
                </asp:WebPartConnection>
            </StaticConnections>
        </asp:webpartmanager>
        <asp:webpartzone id="WebPartZone1" runat="server">
          <zoneTemplate>
            <aspSample:TableProviderWebPart ID="provider1" runat="server" 
              ToolTip="Web Parts Table Provider Control" />
            <aspSample:TableConsumer ID="consumer1" runat="server" 
              ToolTip="Web Parts Table Consumer Control"/>
          </zoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>

ブラウザーでページを読み込みます。 コントロール間の接続は既に存在し、接続がページで静的接続として宣言されているため、コンシューマーはプロバイダーからのデータを表示します。

注釈

ConsumerConnectionPoint クラスのConsumerConnectionPoint コンストラクターは、単純に基底コンストラクターを呼び出し、それにさまざまなパラメーターを渡し、基底クラスを初期化します。

基底クラスのコンストラクターは、接続ポイントのパラメーターの数をチェックし、いくつかの例外をスローできます。 考えられる例外の一覧については、「例外」セクションを参照してください。

ConsumerConnectionPoint コンストラクターを呼び出して、ConsumerConnectionPoint クラスの独自のインスタンスを作成できます。 ただし、単に接続を確立し、クラスを拡張しない場合は、 GetConsumerConnectionPoints メソッドを呼び出して、プロバイダーから接続ポイント オブジェクトを返す必要があります。

適用対象

こちらもご覧ください