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 資料夾中,其將在執行時間動態編譯。 此程式碼範例會使用動態編譯。 如需示範如何編譯的逐步解說,請參閱逐步解說 :開發和使用自訂 Web 服務器控制項

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 實例提供這些資料。

備註

此介面的設計目的是要與網頁元件連線搭配使用。 在網頁元件連線中,位於區域中的兩個 WebPartZoneBase 伺服器控制項會建立連線並共用資料,其中一個控制項做為取用者,另一個控制項做為提供者。 在網頁元件連線中共用資料的機制是介面實例,提供者會透過回呼方法提供給取用者。 若要建立連線,取用者和提供者必須使用相同的介面類別型來共用資料。 如果取用者無法辨識提供者所傳送的介面類別型,仍然可以透過轉換程式連接控制項, WebPartTransformer (物件) 將提供者所傳送的介面實例轉譯成取用者辨識的類型。 如需連線的詳細資訊,請參閱 WebPartConnectionWeb 元件連線概觀

介面 IWebPartRow 是網頁元件控制項所隨附的提供者介面,其設定為根據資料列建立連線的標準介面。 您也可以建立自訂介面來與 Web 元件連線搭配使用,但在許多資料驅動 Web 應用程式中,根據常見的欄位 (建立連線會很有用,如需詳細資訊,請參閱 IWebPartField 介面) 、資料表 (以取得詳細資料、請參閱 IWebPartTable 介面) 或資料來源的資料列。 在一般連接中, WebPart 做為提供者的控制項會實 IWebPartRow 作 介面,並在特殊回呼方法中為取用者提供介面的實例。 例如,提供者可能會針對對應至使用者資訊表中使用者的資料列實 IWebPartRow 作介面。 另一 WebPart 個做為取用者的控制項會定義特殊的方法來接收介面實例,然後擷取使用者資料、使用它來查閱該使用者帳戶的其他資訊,並在頁面上顯示與該使用者相關的所有資訊。

介面 IWebPartRow 有兩個公開的成員。 屬性會 Schema 傳回物件中封裝之資料列的 PropertyDescriptorCollection 架構資訊。 方法 GetRowData 會宣告實作者 (的方法,例如提供者控制項,) 叫用回呼方法時擷取介面實例的資料列資料。

屬性

Schema

取得資料列的結構描述資訊,這個資料列是用於兩個 WebPart 控制項之間共用資料。

方法

GetRowData(RowCallback)

傳回資料列的資料,介面正在將此資料列用作兩個 WebPart 控制項之間連接的基礎。

適用於

另請參閱