ConnectionProviderAttribute Class
Assembly: System.Web (in system.web.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.Method)> _ Public Class ConnectionProviderAttribute Inherits Attribute 'Usage Dim instance As ConnectionProviderAttribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Method) */ public class ConnectionProviderAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Method) public class ConnectionProviderAttribute extends Attribute
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 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. 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).
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. 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). Every Web Parts connection includes a provider connection point that is associated with the provider control.
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). 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. Most of the properties for a provider connection point can also be set programmatically; setting them using the ConnectionProviderAttribute element is optional.
Note |
|---|
| 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). 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. 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. |
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. Note that the simplest overload of the constructor is used; only the displayName parameter value is supplied.
<ConnectionProvider("Row")> _ Public Function GetConnectionInterface() As IWebPartRow Return New RowProviderWebPart() End Function 'GetConnectionInterface
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. Notice that a method is designated as the callback method with the ConnectionProviderAttribute metadata element.
Imports System 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 'New <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 'GetRowData
The second example shows a class acting as a consumer.
Imports System 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 'GetRowData 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 'OnPreRender 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 'RenderContents <ConnectionConsumer("Row")> _ Public Sub SetConnectionInterface(ByVal provider As IWebPartRow) _provider = provider End Sub 'SetConnectionInterface '}
The final example shows the ASP.NET page that contains the two controls.
<%@ page language="VB" %> <%@ Register TagPrefix=my Namespace=MyCustomWebPart %> <html> <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 Title="Row Provider Control" /> <my:RowConsumerWebPart ID=consumer1 runat=server Title="Row Consumer Control" /> </ZoneTemplate> </asp:webpartzone> </div> </form> </body> </html>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note