Export (0) Print
Expand All

ConnectionProviderAttribute Class

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.

System.Object
  System.Attribute
    System.Web.UI.WebControls.WebParts.ConnectionProviderAttribute

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

[AttributeUsageAttribute(AttributeTargets.Method)]
public class ConnectionProviderAttribute : Attribute

The ConnectionProviderAttribute type exposes the following members.

  NameDescription
Public methodConnectionProviderAttribute(String)Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name for the provider connection point.
Public methodConnectionProviderAttribute(String, String)Initializes a new instance of the ConnectionProviderAttribute class, specifying a display name and an ID for the provider connection point.
Public methodConnectionProviderAttribute(String, Type)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.
Public methodConnectionProviderAttribute(String, String, Type)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.
Top

  NameDescription
Public propertyAllowsMultipleConnectionsGets or sets a value that indicates whether the connection point allows multiple connections.
Public propertyConnectionPointTypeGets the type of the connection point associated with a provider control.
Public propertyDisplayNameGets the friendly name of the provider connection point.
Protected propertyDisplayNameValueGets or sets the string used as the value of the DisplayName property, for use in localization scenarios.
Public propertyIDGets a string that represents the unique identity of the provider connection point object.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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.

NoteNote

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 IWebPartRow GetConnectionInterface()
		{
            return new RowProviderWebPart();
        }

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.

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);
		}
    }
}

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;
        }
         }

    }
//}

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>

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft