ConnectionConsumerAttribute クラス

定義

Web パーツ接続でコンシューマーとして動作するサーバー コントロールのコールバック メソッドを識別し、コンシューマーのコネクション ポイントに関する詳細を開発者が指定できるようにします。

public ref class ConnectionConsumerAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method)]
public class ConnectionConsumerAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method)>]
type ConnectionConsumerAttribute = class
    inherit Attribute
Public Class ConnectionConsumerAttribute
Inherits Attribute
継承
ConnectionConsumerAttribute
属性

次のコード例では、 クラスを ConnectionConsumerAttribute 使用して、コンシューマー コントロールのコールバック メソッドでメタデータ要素を宣言 ConnectionConsumerAttribute する方法を示します。 コンストラクターの最も単純なオーバーロードが使用されることに注意してください。 displayName パラメーター値のみが指定されます。

[ConnectionConsumer("Row")]
public void SetConnectionInterface(IWebPartRow provider) 
{
    _provider = provider;
}
    <ConnectionConsumer("Row")> _
    Public Sub SetConnectionInterface(ByVal provider As IWebPartRow)
        _provider = provider

    End Sub
End Class

次のコード例は、 クラスを使用して 2 つの Web パーツ コントロール間の基本的な静的接続を作成する方法を WebPartConnection 示しています。 プロバイダー コード ファイルとコンシューマー コード ファイルは、.aspx ページを含むアプリケーション フォルダーの下にある App_Code フォルダーに配置する必要があります。

最初の例は、プロバイダーとして機能するクラスを示しています。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Web.UI;
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 row data.
namespace My 
{
    public sealed class RowProviderWebPart : WebPart, IWebPartRow 
    {

        private DataTable _table;

        public RowProviderWebPart() 
        {
            _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);
        }
        [ConnectionProvider("Row")]
        public IWebPartRow GetConnectionInterface()
        {
            return new RowProviderWebPart();
        }
        public PropertyDescriptorCollection Schema 
        {
            get {
                return TypeDescriptor.GetProperties(_table.DefaultView[0]);
                }
        }
        public void GetRowData(RowCallback callback)
        {
            callback(_table.Rows);
        }
    }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

'This sample code creates a Web Parts control that acts as a provider of row data.
Namespace MyCustomWebPart

    Public NotInheritable Class RowProviderWebPart
        Inherits WebPart
        Implements IWebPartRow

        Private _table As DataTable


        Public Sub New()
            _table = New DataTable()

            Dim col As New DataColumn()
            col.DataType = GetType(String)
            col.ColumnName = "Name"
            _table.Columns.Add(col)

            col = New DataColumn()
            col.DataType = GetType(String)
            col.ColumnName = "Address"
            _table.Columns.Add(col)

            col = New DataColumn()
            col.DataType = GetType(Integer)
            col.ColumnName = "ZIP Code"
            _table.Columns.Add(col)

            Dim row As DataRow = _table.NewRow()
            row("Name") = "John Q. Public"
            row("Address") = "123 Main Street"
            row("ZIP Code") = 98000
            _table.Rows.Add(row)

        End Sub

        <ConnectionProvider("Row")> _
        Public Function GetConnectionInterface() As IWebPartRow
            Return New RowProviderWebPart()

        End Function 'GetConnectionInterface

        Public ReadOnly Property Schema() As PropertyDescriptorCollection _
            Implements IWebPartRow.Schema
            Get
                Return TypeDescriptor.GetProperties(_table.DefaultView(0))
            End Get
        End Property

        Public Sub GetRowData(ByVal callback As RowCallback) _
            Implements IWebPartRow.GetRowData
            callback(_table.Rows)

        End Sub
    End Class

2 番目の例は、コンシューマーとして機能するクラスを示しています。 メソッドがメタデータ要素を持つコールバック メソッドとして指定されていることに ConnectionConsumerAttribute 注意してください。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
// This sample code creates a Web Parts control that acts as a consumer of row data.
namespace My 
{

    public sealed class RowConsumerWebPart : WebPart {
        private IWebPartRow _provider;
        private ICollection _tableData;
    
            private void GetRowData(object rowData)
            {
                _tableData = (ICollection)rowData;
            }

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

        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("Row")]
        public void SetConnectionInterface(IWebPartRow provider) 
        {
            _provider = provider;
        }
         }
    }
//}
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

' This sample code creates a Web Parts control that acts as a consumer of row data.
Namespace MyCustomWebPart

    Public NotInheritable Class RowConsumerWebPart
        Inherits WebPart
        Private _provider As IWebPartRow
        Private _tableData As ICollection


        Private Sub GetRowData(ByVal rowData As Object)
            _tableData = CType(rowData, ICollection)

        End Sub


        Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
            If Not (_provider Is Nothing) Then
                '        _provider.GetRowData(AddressOf (New RowCallback(GetRowData)))
                _provider.GetRowData(AddressOf GetRowData)
                '    _provider.GetRowData(New RowCallback(AddressOf GetRowData))
            End If

        End Sub



        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            If Not (_provider Is Nothing) Then
                Dim props As PropertyDescriptorCollection = _provider.Schema
                Dim count As Integer = 0
                If Not (props Is Nothing) AndAlso props.Count > 0 AndAlso Not (_tableData Is Nothing) Then
                    Dim prop As PropertyDescriptor
                    For Each prop In props
                        Dim o As DataRow
                        For Each o In _tableData
                            writer.Write(prop.DisplayName & ": " & o(count))
                            writer.WriteBreak()
                            writer.WriteLine()
                            count = count + 1
                        Next o
                    Next prop
                Else
                    writer.Write("No data")
                End If
            Else
                writer.Write("Not connected")
            End If

        End Sub

        <ConnectionConsumer("Row")> _
        Public Sub SetConnectionInterface(ByVal provider As IWebPartRow)
            _provider = provider

        End Sub
    End Class

最後の例では、2 つのコントロールを含む ASP.NET ページを示します。

<%@ page language="C#" %>
<%@ register TagPrefix="my" Namespace="My" %>

<!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>IRow Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <!-- A static or dynamic connection is required to link two Web Parts controls. --->
        <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>
                <!-- The following two lines specify the two connected controls. --->
                <my:RowProviderWebPart ID="provider1" runat="server" ToolTip="Row Provider Control" />
                <my:RowConsumerWebPart ID="consumer1" runat="server" ToolTip="Row Consumer Control" />
            </ZoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>
<%@ page language="VB" %>
<%@ Register TagPrefix="my" Namespace="MyCustomWebPart" %>

<!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>IRow Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <!-- A static or dynamic connection is required to link two Web Parts controls. --->
        <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>
                <my:RowProviderWebPart ID="provider1" runat="server" ToolTip="Row Provider Control" />
                <my:RowConsumerWebPart ID="consumer1" runat="server" ToolTip="Row Consumer Control" />
           </ZoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>

注釈

Web パーツ接続は、1 つのコントロールから他方のコントロールに渡されるインターフェイス インスタンスを使用して、ゾーンに WebPartZoneBase 存在し、データを共有する 2 つのサーバー コントロールで構成されます。 インターフェイス インスタンスを提供するコントロールはプロバイダーと呼ばれ、インターフェイス インスタンスを受け取り、データを処理または表示するコントロールはコンシューマーと呼ばれます。 接続の詳細については、クラスと Web パーツ接続WebPartConnection概要に関するページを参照してください。

接続 WebPart 内のコンシューマー コントロールは、コントロールまたは任意の種類のサーバー コントロールまたはユーザー コントロールにすることができますが、コールバック メソッドとして指定されたメソッドが必要です。 コールバック メソッドは接続プロセス中に呼び出され、その目的は、データを含むインターフェイス インスタンスをプロバイダーから受信することです。 コンシューマーのコールバック メソッドとして機能するメソッドを指定するには、メタデータ要素を メソッドに追加 ConnectionConsumerAttribute する必要があります (要素は クラスに ConnectionConsumerAttribute 基づいています)。

オブジェクトでは、コンシューマーでコールバック メソッドを指定するだけでなく、 ConnectionConsumerAttribute コンシューマーの接続ポイントに関する特定の詳細を指定することもできます。 コンシューマー接続ポイントは、接続を ConsumerConnectionPoint 確立するために必要なコンシューマーに関するすべての詳細 (コンシューマーのコントロールの種類、同時に複数のプロバイダーに接続できるかどうか、プロバイダーからコンシューマーが受信できるインターフェイスの種類、コールバック メソッドの詳細、ユーザー インターフェイス (UI) 内のコンシューマー接続ポイントを表す表示名など) をカプセル化する クラスのインスタンスです。 すべての Web パーツ接続には、コンシューマー コントロールに関連付けられているコンシューマー接続ポイントが含まれます。

メタデータ要素を ConnectionConsumerAttribute コンシューマーのコールバック メソッドに追加する場合は、コンシューマー接続ポイントに関する次の詳細を指定することもできます。接続ポイントの表示名 (詳細については、 プロパティを参照) DisplayName 、コンシューマーが同時に複数のプロバイダーに接続できるかどうか (詳細については、プロパティを AllowsMultipleConnections 参照してください)。 接続ポイントの ID (詳細については、 プロパティを ID 参照)、およびコンシューマーが使用する接続ポイントの種類 (詳細については、 プロパティを ConnectionPointType 参照) を参照してください。 クラスのコンストラクター ConnectionConsumerAttribute の 4 つのオーバーロードには、各クラスの新しいインスタンスが作成されたときに、これらの接続ポイント プロパティの 1 つ以上の値を指定できるパラメーターがあります。 コンシューマー接続ポイントのほとんどのプロパティは、プログラムで設定することもできます。要素を ConnectionConsumerAttribute 使用して設定することは省略可能です。

Note

メタデータ要素をコンシューマーの ConnectionConsumerAttribute コールバック メソッドに追加する場合、常に指定する必要がある唯一の必須パラメーターは パラメーターです displayName (詳細については、コンストラクターのオーバーロードを ConnectionConsumerAttribute(String) 参照してください)。 このパラメーターの値は プロパティに DisplayName 割り当てられ、ユーザーが (コントロールによって作成された ConnectionsZone ) 接続 UI を開くと、表示名は UI のコンシューマー接続ポイントを表します。 コンシューマー コントロールで複数のコールバック メソッドを指定する場合は、選択できる接続ポイントが複数あり、各コールバック メソッドにメタデータ要素を追加 ConnectionConsumerAttribute するときは、 パラメーターの値 id も指定して、各コンシューマー接続ポイントに既知の一意識別子が設定されるようにする必要があります。

コンストラクター

ConnectionConsumerAttribute(String)

コンシューマー接続ポイントの表示名を指定して、ConnectionConsumerAttribute クラスの新しいインスタンスを初期化します。

ConnectionConsumerAttribute(String, String)

コンシューマー接続ポイントの表示名と ID を指定して、ConnectionConsumerAttribute クラスの新しいインスタンスを初期化します。

ConnectionConsumerAttribute(String, String, Type)

コンシューマー接続ポイントに使用する接続ポイント オブジェクトの表示名、ID、および特定の型を指定して、ConnectionConsumerAttribute クラスの新しいインスタンスを初期化します。

ConnectionConsumerAttribute(String, Type)

表示名およびコンシューマー接続ポイントに使用する特定の種類のコネクション ポイント オブジェクトを指定して、ConnectionConsumerAttribute クラスの新しいインスタンスを初期化します。

プロパティ

AllowsMultipleConnections

コネクション ポイントが複数の接続を受け入れるかどうかを示す値を取得または設定します。

ConnectionPointType

コンシューマー接続ポイントのコネクション ポイントの種類を取得します。

DisplayName

コンシューマー接続ポイントの表示名を取得します。

DisplayNameValue

ローカリゼーションで使用するために、DisplayName プロパティの値として使用する文字列を取得または設定します。

ID

コンシューマー接続ポイントの一意の ID を表す文字列を取得します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

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

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください