ConnectionProviderAttribute 类

定义

标识作为 Web 部件连接中的提供者的服务器控件中的回调方法,并使开发人员能指定有关提供者的连接点的详细信息。Identifies the callback method in a server control acting as the provider in a Web Parts connection, and enables developers to specify details about the provider's connection point.

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

示例

下面的代码示例演示如何使用 ConnectionProviderAttribute 类,方法是演示如何 ConnectionProviderAttribute 在提供程序控件中的回调方法上声明 metadata 元素。The following code example demonstrates using the ConnectionProviderAttribute class, by showing how to declare the ConnectionProviderAttribute metadata element on a callback method in a provider control. 请注意,使用构造函数的最简单重载;仅 displayName 提供参数值。Note that the simplest overload of the constructor is used; only the displayName parameter value is supplied.

[ConnectionProvider("Row")]
public IWebPartRow GetConnectionInterface()
{
    return new RowProviderWebPart();
}
<ConnectionProvider("Row")> _
Public Function GetConnectionInterface() As IWebPartRow
    Return New RowProviderWebPart()

End Function 'GetConnectionInterface

下面的代码示例演示如何使用类在两个 Web 部件控件之间创建基本静态连接 WebPartConnectionThe following code examples demonstrate how to create a basic, static connection between two Web Parts controls using the WebPartConnection class. 提供程序和使用者代码文件应放在包含 .aspx 页面的应用程序文件夹下的 App_Code 文件夹中。The provider and consumer code files should be put into the App_Code folder under the application folder that contains the .aspx page.

第一个示例显示一个充当提供程序的类。The first example shows a class acting as a provider. 请注意,方法被指定为具有 metadata 元素的回调方法 ConnectionProviderAttributeNotice that a method is designated as the callback method with the ConnectionProviderAttribute metadata element.

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

第二个示例演示作为使用者的类。The second example shows a class acting as a consumer.

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 页。The final example shows the ASP.NET page that contains the two controls.

<%@ 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 ,并通过从一个控件传递到另一个控件的接口实例来共享数据。A Web Parts connection consists of two server controls residing in a WebPartZoneBase zone and sharing data by means of an interface instance passed from one control to the other. 提供接口实例的控件称为提供程序,接收接口实例并处理或显示数据的控件称为使用者。The control that serves the interface instance is called the provider, and the control that receives the interface instance and processes or displays the data is called the consumer. 有关连接的详细信息,请参阅 WebPartConnection 类和 Web 部件连接概述For details on connections, see the WebPartConnection class and Web Parts Connections Overview.

连接中的提供程序控件可以是 WebPart 控件或任意类型的服务器或用户控件,但它必须具有指定为回调方法的方法。The provider control in a connection can be a WebPart control or any type of server or user control, but it must have a method designated as a callback method. 在连接过程中调用回调方法,其用途是返回给使用者,该实例包含数据。The callback method is invoked during the connection process, and its purpose is to return to the consumer an interface instance that contains data. 若要指定在提供程序中充当回调方法的方法,必须将 ConnectionProviderAttribute 元数据元素添加到方法 (元素基于 ConnectionProviderAttribute 类) 。To designate the method that serves as the callback method in a provider, you must add a ConnectionProviderAttribute metadata element to the method (the element is based on the ConnectionProviderAttribute class).

除了指定提供程序中的回调方法, ConnectionProviderAttribute 对象还允许您指定有关提供程序的连接点的某些详细信息。In addition to designating the callback method in a provider, the ConnectionProviderAttribute object also enables you to specify certain details about a provider's connection point. 提供程序连接点是类的一个实例, ProviderConnectionPoint 它封装了建立连接所需的提供程序的所有详细信息,包括提供程序的控件类型、它是否可以同时连接到多个使用者、提供程序提供给使用者的接口类型、有关回调方法的详细信息以及表示用户界面中的提供者连接点的显示名称 (UI) 。A provider connection point is an instance of the ProviderConnectionPoint class that encapsulates all the details about a provider needed to establish a connection, including the provider's control type, whether it can connect to multiple consumers at the same time, what type of interface the provider serves to consumers, details about the callback method, and a display name that represents the provider connection point in the user interface (UI). 每个 Web 部件连接都包含与提供程序控件关联的提供者连接点。Every Web Parts connection includes a provider connection point that is associated with the provider control.

ConnectionProviderAttribute metadata 元素添加到提供程序中的回调方法时,还可以使用它来指定有关提供程序连接点的以下详细信息:连接点的显示名称 (的详细信息,请参阅 DisplayName 属性) 、提供程序是否可以同时连接到多个使用者 (有关详细信息,请参阅 AllowsMultipleConnections 属性) ,连接点的 ID (详细信息,请参阅 ID 属性) ,以及提供程序使用的连接点的类型 (详细信息,请参阅 ConnectionPointType 属性) 。When you add the ConnectionProviderAttribute metadata element to the callback method in a provider, you can also use it to specify the following details about the provider connection point: a display name for the connection point (for details, see the DisplayName property), whether the provider can connect to multiple consumers at the same time (for details, see the AllowsMultipleConnections property), an ID for the connection point (for details, see the ID property), and the type of the connection point that the provider uses (for details, see the ConnectionPointType property). 该类的构造函数的四个重载 ConnectionProviderAttribute 每个重载都具有参数,这些参数可用于在创建类的新实例时指定这些连接点属性中的一个或多个值。The four overloads of the constructor for the ConnectionProviderAttribute class each have parameters that allow you to specify values for one or more of these connection point properties when a new instance of the class is created. 提供程序连接点的大多数属性还可以通过编程方式进行设置;使用元素设置这些 ConnectionProviderAttribute 元素是可选的。Most of the properties for a provider connection point can also be set programmatically; setting them using the ConnectionProviderAttribute element is optional.

备注

ConnectionProviderAttribute metadata 元素添加到提供程序中的回调方法时,必须始终指定的唯一必需参数是 displayName (详细信息,请参阅 ConnectionProviderAttribute(String) 构造函数重载) 。When you add the ConnectionProviderAttribute metadata element to a callback method in a provider, the only required parameter that you must always specify is the displayName parameter (for details, see the ConnectionProviderAttribute(String) constructor overload). 此参数的值分配给 DisplayName 属性,当用户打开 (控件创建的连接用户界面时 ConnectionsZone) ,显示名称表示 UI 中的提供者连接点。The value of this parameter is assigned to the DisplayName property, and when a user opens the connection UI (created by the ConnectionsZone control), the display name represents the provider connection point in the UI. 如果您在一个提供程序控件中指定多个回调方法,您将有多个可能的连接点可供选择,并且在您向 ConnectionProviderAttribute 每个回调方法添加元数据元素时,还应为该参数指定一个值 id ,以便每个提供程序连接点都具有已知的唯一标识符。If you designate multiple callback methods in a provider control, you will have multiple possible connection points to choose from, and when you add the ConnectionProviderAttribute metadata element to each callback method, you should also specify a value for the id parameter, so that each provider connection point has a known, unique identifier.

构造函数

ConnectionProviderAttribute(String)

初始化 ConnectionProviderAttribute 类的新实例,为提供者连接点指定显示名称。Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name for the provider connection point.

ConnectionProviderAttribute(String, String)

初始化 ConnectionProviderAttribute 类的新实例,为提供者连接点指定显示名称和 ID。Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name and an ID for the provider connection point.

ConnectionProviderAttribute(String, String, Type)

初始化 ConnectionProviderAttribute 类的新实例,指定显示名称、ID 以及用于提供程序连接点的连接点对象的特定类型。Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name, an ID, and a specific type of connection point object to use for the provider connection point.

ConnectionProviderAttribute(String, Type)

初始化 ConnectionProviderAttribute 类的新实例,指定用于提供者连接点的连接点对象的显示名称和特定类型。Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name and a specific type of connection point object to use for the provider connection point.

属性

AllowsMultipleConnections

获取或设置一个值,该值指示连接点是否允许多个连接。Gets or sets a value that indicates whether the connection point allows multiple connections.

ConnectionPointType

获取与提供者控件关联的连接点的类型。Gets the type of the connection point associated with a provider control.

DisplayName

获取提供者连接点的友好名称。Gets the friendly name of the provider connection point.

DisplayNameValue

获取或设置用作 DisplayName 属性值的字符串,用于本地化方案中。Gets or sets the string used as the value of the DisplayName property, for use in localization scenarios.

ID

获取一个字符串,该字符串表示提供者连接点对象的唯一标识。Gets a string that represents the unique identity of the provider connection point object.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅