Export (0) Print
Expand All

ConsumerConnectionPoint Constructor

Initializes a new instance of the ConsumerConnectionPoint class.

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

public:
ConsumerConnectionPoint(
	MethodInfo^ callbackMethod, 
	Type^ interfaceType, 
	Type^ controlType, 
	String^ displayName, 
	String^ id, 
	bool allowsMultipleConnections
)

Parameters

callbackMethod
Type: System.Reflection::MethodInfo

The method in the consumer control that returns an interface instance to consumers to establish a connection.

interfaceType
Type: System::Type

The Type of the interface that the consumer receives from a provider.

controlType
Type: System::Type

The Type of the consumer control with which the consumer connection point is associated.

displayName
Type: System::String

A friendly display name for the consumer connection point that appears to users in the connection user interface (UI).

id
Type: System::String

A unique identifier for the consumer connection point.

allowsMultipleConnections
Type: System::Boolean

A Boolean value indicating whether the consumer connection point can have multiple simultaneous connections with providers.

ExceptionCondition
ArgumentNullException

callbackMethod is nullptr.

- or -

interfaceType is nullptr.

- or -

controlType is nullptr.

- or -

displayName is nullptr or an empty string ("").

ArgumentException

controlType is not the same type as the consumer control (or a valid class derived from it).

The ConsumerConnectionPoint constructor for the ConsumerConnectionPoint class simply calls the base constructor, passing to it the various parameters and initializing the base class.

The base class constructor checks a number of the parameters for a connection point and can throw several exceptions. For a list of possible exceptions, see the Exceptions section.

You can call the ConsumerConnectionPoint constructor to create your own instance of the ConsumerConnectionPoint class. However, in cases where you are simply establishing a connection and not extending the class, you should call the GetConsumerConnectionPoints method to return a connection point object from a provider.

The following code example demonstrates how to derive from the ConsumerConnectionPoint class to create a custom provider connection point.

The code example has three parts:

  • A source file that contains a provider WebPart control, a consumer WebPart control, and a custom ConsumerConnectionPoint object.

  • A Web page that hosts the controls in a static connection.

  • An explanation of how to run the example code.

The first part of the code example is the source for the provider and consumer WebPart controls, and a custom ConsumerConnectionPoint class, named TableConsumerConnectionPoint. Note that the constructor of the TableConsumerConnectionPoint class calls the base constructor, passing it the required parameters as indicated in the Parameters section. Also note that in the TableConsumer class, the SetConnectionInterface method is specified as the callback method for connections, and the ConnectionConsumer attribute declares the custom TableConsumerConnectionPoint as a parameter. This demonstrates how to create a custom consumer connection point and then associate it with a consumer control. This example assumes that the source code is dynamically compiled, so you should place the source code file in an App_Code subfolder of your Web application.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
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 table data. 
namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
	public sealed class TableProviderWebPart : WebPart, IWebPartTable
	{
		DataTable _table;

		public TableProviderWebPart()
		{
			_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);
		}

		public PropertyDescriptorCollection Schema
		{
			get
			{
				return TypeDescriptor.GetProperties(_table.DefaultView[0]);
			}
		}
		public void GetTableData(TableCallback callback)
		{
				callback(_table.Rows);
		}

		public bool ConnectionPointEnabled
		{
			get
			{
				object o = ViewState["ConnectionPointEnabled"];
				return (o != null) ? (bool)o : true;
			}
			set
			{
				ViewState["ConnectionPointEnabled"] = value;
			}
		}

		[ConnectionProvider("Table", typeof(TableProviderConnectionPoint), AllowsMultipleConnections = true)]
		public IWebPartTable GetConnectionInterface()
		{
			return new TableProviderWebPart();
		}

		public class TableProviderConnectionPoint : ProviderConnectionPoint
		{
			public TableProviderConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType,
			string name, string id, bool allowsMultipleConnections) : base(
				callbackMethod, interfaceType, controlType,
				name, id, allowsMultipleConnections)
			{
			}
			public override bool GetEnabled(Control control)
			{
				return ((TableProviderWebPart)control).ConnectionPointEnabled;
			}
		}
	}
	
	// This code sample demonstrates a custom WebPart controls that acts as  
	// a consumer in a Web Parts connection.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TableConsumer : WebPart
  {
    private IWebPartTable _provider;
    private ICollection _tableData;

    private void GetTableData(object tableData)
    {
      _tableData = (ICollection)tableData;
    }

    protected override void OnPreRender(EventArgs e)
    {
      if (_provider != null)
      {
        _provider.GetTableData(new TableCallback(GetTableData));
      }
    }


    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("Table")]
    public void SetConnectionInterface(IWebPartTable provider)
    {
      _provider = provider;
    }

    public class TableConsumerConnectionPoint : ConsumerConnectionPoint
    {
      public TableConsumerConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType,
      string name, string id, bool allowsMultipleConnections)
        : base(
        callbackMethod, interfaceType, controlType,
        name, id, allowsMultipleConnections)
      {
      }
    }
  }
}

The second part of the code example is the Web page that hosts the custom controls in a static Web parts connection. At the top of the page is a Register directive to declare a prefix and the namespace for the custom controls. The connection is declared by using an <asp:webpartconnection> element, and the provider and consumer controls are declared within an <asp:webpartzone> element.

<%@ page language="C#" %>
<%@ register tagprefix="aspSample" 
    namespace="Samples.AspNet.CS.Controls" %>

<!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>IField Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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>
            <aspSample:TableProviderWebPart ID="provider1" runat="server" 
              ToolTip="Web Parts Table Provider Control" />
            <aspSample:TableConsumer ID="consumer1" runat="server" 
              ToolTip="Web Parts Table Consumer Control"/>
          </zoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>

Load the page in a browser. The connection between the controls already exists, and the consumer displays the data from the provider, because the connection was declared as a static connection in the page.

.NET Framework

Supported in: 4.5, 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.

Show:
© 2014 Microsoft