語言

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

一個布林值,表示消費者連接點是否能同時與供應商連接多個服務提供者。

例外狀況

callbackMethod 是 null。

-或-

interfaceType 是 null。

-或-

controlType 是 null。

-或-

displayName 是 null 空字串(“”)。

controlType 與消費者控制(或其衍生的有效類別)不同。

範例

以下程式碼範例示範如何從 ConsumerConnectionPoint 類別推導出,建立自訂的提供者連接點。

該程式碼範例包含三個部分:

  • 一個包含提供者 WebPart 控制、消費者 WebPart 控制和自訂 ConsumerConnectionPoint 物件的原始碼檔案。

  • 一個以靜態連線方式承載控制項的網頁。

  • 說明如何執行範例程式碼。

程式碼範例的第一部分是提供者與消費者WebPart控制的原始碼,以及一個名為 ConsumerConnectionPoint的自訂TableConsumerConnectionPoint類別。 請注意, TableConsumerConnectionPoint 類別的建構子會呼叫基礎建構子,並依參數章節所示傳遞所需的參數。 另外請注意,在類別TableConsumerSetConnectionInterface中,該方法被指定為連線的回調方法,屬性ConnectionConsumer則以參數宣告自訂TableConsumerConnectionPoint。 這示範了如何建立自訂的消費者連接點,並將其與消費者控制系統關聯起來。 這個例子假設原始碼是動態編譯的,所以你應該把原始碼檔案放在網頁應用程式的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 這個方法,從提供者回傳一個連接點物件。

適用於

另請參閱