ConnectionProviderAttribute 类

定义

标识作为 Web 部件连接中的提供者的服务器控件中的回调方法,并使开发人员能指定有关提供者的连接点的详细信息。

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 类。 请注意,使用构造函数的最简单重载; displayName 仅提供参数值。

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

End Function 'GetConnectionInterface

下面的代码示例演示如何使用 WebPartConnection 类在两个 Web 部件控件之间创建基本的静态连接。 提供程序和使用者代码文件应放入包含 .aspx 页的应用程序文件夹下的 App_Code 文件夹中。

第一个示例演示充当提供程序的类。 请注意,方法被指定为具有元数据元素的 ConnectionProviderAttribute 回调方法。

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

第二个示例演示充当使用者的类。

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 控件或任何类型的服务器或用户控件,但它必须具有指定为回调方法的方法。 回调方法在连接过程中调用,其用途是向使用者返回包含数据的接口实例。 若要在提供程序中指定用作回调方法的方法,必须将元数据元素添加到 ConnectionProviderAttribute 方法, (元素基于 ConnectionProviderAttribute 类) 。

除了在提供程序中指定回调方法外, ConnectionProviderAttribute 对象还允许您指定有关提供程序的连接点的某些详细信息。 提供程序连接点是 类的 ProviderConnectionPoint 一个实例,它封装建立连接所需的提供程序的所有详细信息,包括提供程序的控件类型、是否可以同时连接到多个使用者、提供程序为使用者服务的接口类型、有关回调方法的详细信息,以及表示用户界面 (UI) 中的提供程序连接点的显示名称。 每个 Web 部件连接都包含一个与提供程序控件关联的提供程序连接点。

ConnectionProviderAttribute 元数据元素添加到提供程序中的回调方法时,还可以使用它来指定有关提供程序连接点的以下详细信息:连接点的显示名称 (有关详细信息,请参阅 DisplayName 属性) ,提供程序是否可以同时连接到多个使用者 (了解详细信息, 有关详细信息, AllowsMultipleConnections 请参阅属性) 、连接点的 ID (、 ID 属性) 以及提供程序使用的连接点的类型 (,有关详细信息,请参阅 ConnectionPointType 属性) 。 类的构造函数 ConnectionProviderAttribute 的四个重载都有参数,这些参数允许在创建类的新实例时为这些连接点属性中的一个或多个指定值。 提供程序连接点的大多数属性也可以以编程方式设置;使用 ConnectionProviderAttribute 元素设置它们是可选的。

注意

ConnectionProviderAttribute 元数据元素添加到提供程序中的回调方法时,必须始终指定的唯一必需参数是 displayName 参数 (,有关详细信息,请参阅 ConnectionProviderAttribute(String) 构造函数重载) 。 此参数的值分配给 DisplayName 属性,当用户打开由控件) 创建的 ConnectionsZone 连接 UI (时,显示名称表示 UI 中的提供程序连接点。 如果在提供程序控件中指定多个回调方法,则有多个可能的连接点可供选择,在将元数据元素添加到 ConnectionProviderAttribute 每个回调方法时,还应为 id 参数指定一个值,以便每个提供程序连接点都有一个已知的唯一标识符。

构造函数

ConnectionProviderAttribute(String)

初始化 ConnectionProviderAttribute 类的新实例,为提供者连接点指定显示名称。

ConnectionProviderAttribute(String, String)

初始化 ConnectionProviderAttribute 类的新实例,为提供者连接点指定显示名称和 ID。

ConnectionProviderAttribute(String, String, Type)

初始化 ConnectionProviderAttribute 类的新实例,指定显示名称、ID 以及用于提供程序连接点的连接点对象的特定类型。

ConnectionProviderAttribute(String, Type)

初始化 ConnectionProviderAttribute 类的新实例,指定用于提供者连接点的连接点对象的显示名称和特定类型。

属性

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)

适用于

另请参阅