Επεξεργασία

Κοινή χρήση μέσω


ConnectionConsumerAttribute Class

Definition

Identifies the callback method in a server control acting as the consumer in a Web Parts connection, and enables developers to specify details about the consumer's connection point.

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
Inheritance
ConnectionConsumerAttribute
Attributes

Examples

The following code example demonstrates using the ConnectionConsumerAttribute class, by showing how to declare the ConnectionConsumerAttribute metadata element on a callback method in a consumer control. Note that the simplest overload of the constructor is used; only the displayName parameter value is supplied.

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

    End Sub
End Class

The following code examples demonstrate how to create a basic, static connection between two Web Parts controls using the WebPartConnection class. 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.

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. Notice that a method is designated as the callback method with the ConnectionConsumerAttribute 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 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

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>

Remarks

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. For details on connections, see the WebPartConnection class and Web Parts Connections Overview.

The consumer 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 receive from the provider an interface instance that contains data. To designate the method that serves as the callback method in a consumer, you must add a ConnectionConsumerAttribute metadata element to the method (the element is based on the ConnectionConsumerAttribute class).

In addition to designating the callback method in a consumer, the ConnectionConsumerAttribute object also enables you to specify certain details about a consumer's connection point. A consumer connection point is an instance of the ConsumerConnectionPoint class that encapsulates all the details about a consumer needed to establish a connection, including the consumer's control type, whether it can connect to multiple providers at the same time, what type of interface the consumer can receive from a provider, details about the callback method, and a display name that represents the consumer connection point in the user interface (UI). Every Web Parts connection includes a consumer connection point that is associated with the consumer control.

When you add the ConnectionConsumerAttribute metadata element to the callback method in a consumer, you can also use it to specify the following details about the consumer connection point: a display name for the connection point (for details, see the DisplayName property), whether the consumer can connect to multiple providers 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 consumer uses (for details, see the ConnectionPointType property). The four overloads of the constructor for the ConnectionConsumerAttribute 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. Most of the properties for a consumer connection point can also be set programmatically; setting them using the ConnectionConsumerAttribute element is optional.

Note

When you add the ConnectionConsumerAttribute metadata element to a callback method in a consumer, the only required parameter that you must always specify is the displayName parameter (for details, see the ConnectionConsumerAttribute(String) constructor overload). 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 consumer connection point in the UI. If you designate multiple callback methods in a consumer control, you will have multiple possible connection points to choose from, and when you add the ConnectionConsumerAttribute metadata element to each callback method, you should also specify a value for the id parameter, so that each consumer connection point has a known, unique identifier.

Constructors

ConnectionConsumerAttribute(String, String, Type)

Initializes a new instance of the ConnectionConsumerAttribute class, specifying a display name, an ID, and a specific type of connection point object to use for the consumer connection point.

ConnectionConsumerAttribute(String, String)

Initializes a new instance of the ConnectionConsumerAttribute class, specifying a display name and an ID for the consumer connection point.

ConnectionConsumerAttribute(String, Type)

Initializes a new instance of the ConnectionConsumerAttribute class, specifying a display name and a specific type of connection point object to use for the consumer connection point.

ConnectionConsumerAttribute(String)

Initializes a new instance of the ConnectionConsumerAttribute class, specifying a display name for the consumer connection point.

Properties

AllowsMultipleConnections

Gets or sets a value that indicates whether the connection point allows multiple connections.

ConnectionPointType

Gets the connection point type of the consumer connection point.

DisplayName

Gets the friendly name of the consumer connection point.

DisplayNameValue

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 consumer connection point.

TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Methods

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode()

Returns the hash code for this instance.

(Inherited from Attribute)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

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

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from 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.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also