ConsumerConnectionPoint 构造函数

定义

初始化 ConsumerConnectionPoint 类的新实例。

public:
 ConsumerConnectionPoint(System::Reflection::MethodInfo ^ callbackMethod, Type ^ interfaceType, Type ^ controlType, System::String ^ displayName, System::String ^ id, bool allowsMultipleConnections);
public ConsumerConnectionPoint (System.Reflection.MethodInfo callbackMethod, Type interfaceType, Type controlType, string displayName, string id, bool allowsMultipleConnections);
new System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint : System.Reflection.MethodInfo * Type * Type * string * string * bool -> System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint
Public Sub New (callbackMethod As MethodInfo, interfaceType As Type, controlType As Type, displayName As String, id As String, allowsMultipleConnections As Boolean)

参数

callbackMethod
MethodInfo

使用者控件中的方法,该方法将接口实例返回给使用者以建立连接。

interfaceType
Type

使用者从提供者接收的接口的 Type

controlType
Type

使用者连接点所关联的使用者控件的 Type

displayName
String

在连接用户界面 (UI) 中向用户显示的使用者连接点的友好显示名称。

id
String

使用者连接点的唯一标识符。

allowsMultipleConnections
Boolean

一个布尔值,指示使用者连接点是否可同时拥有与提供者的多个连接。

例外

callbackMethodnull

  • 或 - interfaceType 上声明的默认值为 null

  • 或 - controlType 上声明的默认值为 null

  • 或 - displayNamenull 或空字符串 ("")。

controlType 与使用者控件(或从使用者控件派生的有效类)的类型不同。

示例

下面的代码示例演示如何从 ConsumerConnectionPoint 类派生以创建自定义提供程序连接点。

代码示例有三个部分:

  • 包含提供程序 WebPart 控件、使用者 WebPart 控件和自定义 ConsumerConnectionPoint 对象的源文件。

  • 承载静态连接中的控件的网页。

  • 有关如何运行示例代码的说明。

代码示例的第一部分是提供程序和使用者WebPart控件的源,以及一TableConsumerConnectionPoint个名为的自定义ConsumerConnectionPoint类。 请注意,类的 TableConsumerConnectionPoint 构造函数调用基构造函数,并按 Parameters 节中指示传递所需的参数。 另请注意,在 TableConsumer 类中,该方法 SetConnectionInterface 被指定为连接的回调方法,并且属性 ConnectionConsumer 将自定义 TableConsumerConnectionPoint 声明为参数。 这演示了如何创建自定义使用者连接点,然后将其与使用者控件相关联。 此示例假定源代码是动态编译的,因此应将源代码文件放置在 Web 应用程序的App_Code子文件夹中。

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

代码示例的第二部分是承载静态 Web 部件连接中的自定义控件的网页。 页面顶部是一个 Register 指令,用于声明自定义控件的前缀和命名空间。 连接是使用 <asp:webpartconnection> 元素声明的,提供程序和使用者控件在元素中 <asp:webpartzone> 声明。

<%@ 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>

在浏览器中加载页面。 控件之间的连接已存在,使用者显示来自提供程序的数据,因为连接在页面中声明为静态连接。

注解

ConsumerConnectionPoint类的ConsumerConnectionPoint构造函数只是调用基构造函数,并传递给基类的各种参数并初始化基类。

基类构造函数检查连接点的多个参数,并可能会引发多个异常。 有关可能的异常的列表,请参阅“异常”部分。

可以调用 ConsumerConnectionPoint 构造函数来创建自己的类实例 ConsumerConnectionPoint 。 但是,如果只是建立连接而不扩展类,则应调用 GetConsumerConnectionPoints 该方法以从提供程序返回连接点对象。

适用于

另请参阅