次の方法で共有


ProviderConnectionPoint コンストラクター

定義

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

public:
 ProviderConnectionPoint(System::Reflection::MethodInfo ^ callbackMethod, Type ^ interfaceType, Type ^ controlType, System::String ^ displayName, System::String ^ id, bool allowsMultipleConnections);
public ProviderConnectionPoint (System.Reflection.MethodInfo callbackMethod, Type interfaceType, Type controlType, string displayName, string id, bool allowsMultipleConnections);
new System.Web.UI.WebControls.WebParts.ProviderConnectionPoint : System.Reflection.MethodInfo * Type * Type * string * string * bool -> System.Web.UI.WebControls.WebParts.ProviderConnectionPoint
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 が、プロバイダー コントロール (またはそれから派生した有効なクラス) と同じ型ではありません。

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

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

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

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

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

コード例の最初の部分は、プロバイダーコントロールとコンシューマー WebPart コントロールのソースと、 という名前TableProviderConnectionPointのカスタム ProviderConnectionPoint クラスです。 クラスのコンストラクターは基本コンストラクターを TableProviderConnectionPoint 呼び出し、「Parameters」セクションで示されているように必要なパラメーターを渡すことに注意してください。 また、 TableProviderWebPart クラスでは、 GetConnectionInterface メソッドが接続のコールバック メソッドとして指定され、 属性によって ConnectionProvider カスタム TableProviderConnectionPoint がパラメーターとして宣言されることにも注意してください。 これは、カスタム プロバイダー接続ポイントを作成し、プロバイダー コントロールに関連付ける方法を示しています。 この例では、ソース コードが動的にコンパイルされていることを前提としているため、ソース コード ファイルを 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>

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

注釈

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

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

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

適用対象

こちらもご覧ください