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

-或- interfaceTypenull

-或- controlTypenull

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

程式碼範例的第二個部分是裝載靜態網頁元件連線中自訂控制項的網頁。 頁面頂端是一個 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>

在瀏覽器中載入頁面。 控制項之間的連接已經存在,而取用者會顯示來自提供者的資料,因為連接在頁面中宣告為靜態連接。

備註

類別 ConsumerConnectionPointConsumerConnectionPoint 建構函式只會呼叫基底建構函式,並傳遞至各種參數並初始化基類。

基類建構函式會檢查連接點的參數數目,而且可能會擲回數個例外狀況。 如需可能例外狀況的清單,請參閱例外狀況一節。

您可以呼叫 建 ConsumerConnectionPoint 構函式來建立自己的 類別實例 ConsumerConnectionPoint 。 不過,如果您只是建立連接而不是擴充 類別,您應該呼叫 GetConsumerConnectionPoints 方法,以從提供者傳回連接點物件。

適用於

另請參閱