語言

IWebPartRow 介面

定義

定義一個提供者介面,用於使用單一資料欄位連接兩個伺服器控制項。

public interface class IWebPartRow
public interface IWebPartRow
type IWebPartRow = interface
Public Interface IWebPartRow

範例

以下程式碼範例示範如何利用介面 IWebPartRow 建立兩個控制項之間的靜態連線。 該程式碼範例包含三個部分:

  • 兩個自訂 WebPart 控制項的原始碼,可透過介面 IWebPartRow 形成連線,一個控制項作為提供者,另一個作為消費者。

  • 一個網頁,負責承載控制項並以持久化格式宣告靜態連線。

  • 說明範例程式碼執行時會發生什麼。

程式碼範例的第一部分是兩個自訂控制項的原始碼。 第一是提供者的程式碼,它實作了 IWebPartRow 介面。 為了簡化,範例中提供者建立一個包含部分資料的資料表,而非連接資料庫。 該 GetConnectionInterface 方法作為提供者的連接點,回調方法將介面實例回傳給消費者。 至於消費者,它會從提供者那裡以名為 SetConnectionInterface的方法取得介面實例,該方法標示為 ConnectionConsumer 屬性。 取得介面實例後,消費者 OnPreRender 在其方法中呼叫提供者中方法的實作 GetRowData ,以取得實際資料並將其寫入頁面。

要執行程式碼範例,你必須編譯這個原始碼。 你可以明確編譯,並將結果組合放到你網站的 Bin 資料夾或全域組合快取中。 或者,你也可以把原始碼放在網站的App_Code資料夾,執行時會動態編譯。 此程式碼範例使用動態編譯。 如需示範如何編譯的攻略,請參閱 「攻略:開發與使用自訂網頁伺服器控制項」。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls 
{
  // This sample code creates a Web Parts control that acts as a provider 
  // of row data.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  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);
        }
  } // RowProviderWebPart

  // This sample code creates a Web Parts control that acts as a consumer 
  // of row data.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]  
  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;
    }
  } // RowConsumerWebPart
} // Samples.AspNet.CS.Controls
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  ' This sample code creates a Web Parts control that acts as a provider 
  ' of row data.
  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public NotInheritable Class RowProviderWebPart
    Inherits WebPart
    Implements IWebPartRow
    Private _table As DataTable


    Public Sub New()
      _table = New DataTable()

      Dim col As New DataColumn()
      col.DataType = GetType(String)
      col.ColumnName = "Name"
      _table.Columns.Add(col)

      col = New DataColumn()
      col.DataType = GetType(String)
      col.ColumnName = "Address"
      _table.Columns.Add(col)

      col = New DataColumn()
      col.DataType = GetType(Integer)
      col.ColumnName = "ZIP Code"
      _table.Columns.Add(col)

      Dim row As DataRow = _table.NewRow()
      row("Name") = "John Q. Public"
      row("Address") = "123 Main Street"
      row("ZIP Code") = 98000
      _table.Rows.Add(row)

    End Sub


    <ConnectionProvider("Row")> _
    Public Function GetConnectionInterface() As IWebPartRow
      Return New RowProviderWebPart()

    End Function 'GetConnectionInterface


    Public ReadOnly Property Schema() As _
      ComponentModel.PropertyDescriptorCollection Implements IWebPartRow.Schema

      Get
        Return TypeDescriptor.GetProperties(_table.DefaultView(0))
      End Get

    End Property


    Public Sub GetRowData(ByVal callback As RowCallback) _
      Implements IWebPartRow.GetRowData
      callback(_table.Rows)

    End Sub

  End Class


  ' This sample code creates a Web Parts control that acts as a consumer 
  ' of row data.
  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public NotInheritable Class RowConsumerWebPart
    Inherits WebPart
    Private _provider As IWebPartRow
    Private _tableData As ICollection


    Private Sub GetRowData(ByVal rowData As Object)
      _tableData = CType(rowData, ICollection)

    End Sub


    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

      If Not (_provider Is Nothing) Then
        _provider.GetRowData(New RowCallback(AddressOf GetRowData))
      End If

    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)

      If Not (_provider Is Nothing) Then
        Dim props As PropertyDescriptorCollection = _provider.Schema
        Dim count As Integer = 0
        If Not (props Is Nothing) AndAlso props.Count > 0 _
          AndAlso Not (_tableData Is Nothing) Then

          Dim prop As PropertyDescriptor
          For Each prop In props
            Dim o As DataRow
            For Each o In _tableData
              writer.Write(prop.DisplayName & ": " & o(count))
              writer.WriteBreak()
              writer.WriteLine()
              count = count + 1
            Next o
          Next prop
        Else
          writer.Write("No data")
        End If
      Else
        writer.Write("Not connected")
      End If

    End Sub


    <ConnectionConsumer("Row")> _
    Public Sub SetConnectionInterface(ByVal provider As IWebPartRow)
      _provider = provider

    End Sub

  End Class

End Namespace  ' Samples.AspNet.VB.Controls

程式碼範例的第二部分是宣告靜態連線並承載控制項的網頁。 頁面頂端附近有一個 Register 指令,宣告該目錄App_Code中原始碼的命名空間。 連線是用元素 <asp:webpartconnection> 宣告的。 自訂的消費者與提供者控制在元素<zonetemplate>中的元素中宣告<asp:webpartzone>,這是它們能夠連接的必要條件(必須位於繼承自WebPartZoneBase該類別的區域內)。

<%@ page language="C#" %>
<%@ Register tagprefix="IRow" 
    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>IRow 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>
            <irow:RowProviderWebPart ID="provider1" runat="server" 
              Title="Row Provider Control" />
            <irow:RowConsumerWebPart ID="consumer1" runat="server" 
              Title="Row Consumer Control" />
          </ZoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>
<%@ page language="VB" %>
<%@ Register tagprefix="IRow" 
    Namespace="Samples.AspNet.VB.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>IRow 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>
            <irow:RowProviderWebPart ID="provider1" runat="server" 
              Title="Row Provider Control" />
            <irow:RowConsumerWebPart ID="consumer1" runat="server" 
              Title="Row Consumer Control" />
          </ZoneTemplate>
        </asp:webpartzone>
    </div>
    </form>
</body>
</html>

在瀏覽器中載入該頁面。 消費者控制顯示來自指定資料列的資料,提供者透過介面實例 IWebPartRow 提供資料。

備註

此介面設計用於 Web Parts 連線。 在 Web Parts 連線中,兩個位於同一 WebPartZoneBase 區域的伺服器控制點建立連線並共享資料,其中一個控制節點扮演消費者,另一個控制器則作為提供者。 Web Parts 連線中資料共享的機制是一個介面實例,提供者透過回調方法向消費者提供該實例。 要建立連線,消費者與提供者必須都使用相同介面類型來分享資料。 如果消費者無法辨識提供者傳送的介面類型,仍可透過變換器(物件 WebPartTransformer )將提供者傳送的介面實例轉換成消費者能辨識的類型來連接控制項。 有關連接的詳細資訊,請參閱WebPartConnection網頁零件連接概覽。

此 IWebPartRow 介面是隨 Web Parts 控制集內建的提供者介面,作為基於資料列建立連線的標準介面。 你也可以建立自訂介面來搭配 Web Parts 連線,但在許多資料驅動的網頁應用程式中,基於共用欄位(詳情見 IWebPartField 介面)、表格(詳情請見 IWebPartTable 介面)或資料來源的列來建立連結是有用的。 在典型的連線中,作為提供者的 WebPart 控制項會實作 IWebPartRow 介面,並以特殊的回調方式提供介面實例給消費者。 例如,提供者可能會實作 IWebPartRow 一個介面,針對你使用者資訊表中的某個使用者。 另一個 WebPart 作為消費者的控制項會定義一種特殊方法來接收介面實例,然後擷取使用者資料,利用它查詢該使用者帳號的額外資訊,並在頁面上顯示所有與該使用者相關的資訊。

介面 IWebPartRow 有兩個裸露元件。 該 Schema 屬性回傳封裝在 PropertyDescriptorCollection 物件中的資料列結構資訊。 該 GetRowData 方法宣告一種方法,實作者(例如提供者控制)在呼叫回調方法時會用它來取得介面實例的列資料。

屬性

名稱 Description
Schema

取得用於兩個控制項間資料共享的資料 WebPart 列的結構資訊。

方法

名稱 Description
GetRowData(RowCallback)

回傳介面所使用的資料列,作為兩個 WebPart 控制項間連接的基礎。

適用於

另請參閱