ConnectionConsumerAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
标识作为 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 类。 请注意,使用构造函数的最简单重载; 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
下面的代码示例演示如何使用 WebPartConnection 类在两个 Web 部件控件之间创建基本的静态连接。 提供程序和使用者代码文件应放入包含.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
第二个示例演示充当使用者的类。 请注意,方法被指定为具有元数据元素的 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
最后一个示例显示了包含两个控件的 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 部件连接由驻留在区域中的两个 WebPartZoneBase 服务器控件组成,并通过从一个控件传递到另一个控件的接口实例共享数据。 为接口实例提供服务的控件称为提供程序,接收接口实例并处理或显示数据的控件称为使用者。 有关连接的详细信息,请参阅 WebPartConnection 类和 Web 部件连接概述。
连接中的使用者控件可以是控件 WebPart 或任何类型的服务器或用户控件,但它必须具有指定为回调方法的方法。 回调方法在连接过程中调用,其用途是从提供程序接收包含数据的接口实例。 若要指定用作使用者中回调方法的方法,必须将元数据元素添加到 ConnectionConsumerAttribute 方法, (元素基于 ConnectionConsumerAttribute 类) 。
除了在使用者中指定回调方法外, ConnectionConsumerAttribute 对象还可用于指定有关使用者连接点的某些详细信息。 使用者连接点是 类的 ConsumerConnectionPoint 一个实例,该类封装建立连接所需的使用者的所有详细信息,包括使用者的控件类型、它是否可以同时连接到多个提供程序、使用者可以从提供程序接收的接口类型、有关回调方法的详细信息,以及表示用户界面 (UI) 中的使用者连接点的显示名称。 每个 Web 部件连接都包含一个与使用者控件关联的使用者连接点。
将 ConnectionConsumerAttribute 元数据元素添加到使用者中的回调方法时,还可以使用它来指定有关使用者连接点的以下详细信息:连接点的显示名称 (有关详细信息,请参阅 DisplayName 属性) ,使用者是否可以同时连接到多个提供程序 (了解详细信息, 有关详细信息, AllowsMultipleConnections 请参阅属性) 、连接点的 ID (、 ID 属性) 以及使用者 (使用的连接点类型,有关详细信息,请参阅 ConnectionPointType 属性) 。 类的四个构造函数 ConnectionConsumerAttribute 重载每个重载都有参数,这些参数允许你在创建类的新实例时为其中一个或多个连接点属性指定值。 使用者连接点的大多数属性也可以以编程方式设置;使用 ConnectionConsumerAttribute 元素设置它们是可选的。
注意
将 ConnectionConsumerAttribute 元数据元素添加到使用者中的回调方法时,必须始终指定的唯一必需参数是 displayName 参数 (有关详细信息,请参阅 ConnectionConsumerAttribute(String) 构造函数重载) 。 此参数的值分配给 DisplayName 属性,当用户打开由控件) 创建的 ConnectionsZone 连接 UI (时,显示名称表示 UI 中的使用者连接点。 如果在使用者控件中指定多个回调方法,则有多个可能的连接点可供选择,并且向每个回调方法添加 ConnectionConsumerAttribute 元数据元素时,还应为 id 参数指定一个值,以便每个使用者连接点都有一个已知的唯一标识符。
构造函数
| ConnectionConsumerAttribute(String) | 
		 初始化 ConnectionConsumerAttribute 类的新实例,为使用者连接点指定显示名称。  | 
        	
| ConnectionConsumerAttribute(String, String) | 
		 初始化 ConnectionConsumerAttribute 类的新实例,为使用者连接点指定显示名称和 ID。  | 
        	
| ConnectionConsumerAttribute(String, String, Type) | 
		 初始化 ConnectionConsumerAttribute 类的新实例,指定显示名称、ID 以及用于使用者连接点的连接点对象的特定类型。  | 
        	
| ConnectionConsumerAttribute(String, Type) | 
		 初始化 ConnectionConsumerAttribute 类的新实例,指定使用者连接点所使用的显示名称和特定连接点对象类型。  | 
        	
属性
| AllowsMultipleConnections | 
		 获取或设置一个值,该值指示连接点是否允许多个连接。  | 
        	
| ConnectionPointType | 
		 获取使用者连接点的连接点类型。  | 
        	
| DisplayName | 
		 获取使用者连接点的友好名称。  | 
        	
| DisplayNameValue | 
		 获取或设置用作 DisplayName 属性值的字符串,用于本地化方案中。  | 
        	
| 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) |