IWebPartRow 接口

定义

定义使用单个字段数据连接两个服务器控件的提供者接口。

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

示例

下面的代码示例演示如何使用 IWebPartRow 接口在两个控件之间创建静态连接。 该代码示例包含三个部分:

  • 可以使用 接口建立连接的IWebPartRow两个自定义WebPart控件的源代码,其中一个控件充当提供程序,另一个控件充当使用者。

  • 承载控件和以持久性格式声明静态连接的网页。

  • 对运行示例代码时发生的情况的说明。

代码示例的第一部分是两个自定义控件的源代码。 第一个是实现 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> 元素声明连接。 自定义使用者控件和提供程序控件在 元素中的<asp:webpartzone>元素中<zonetemplate>声明,这是它们能够连接 (它们必须驻留在继承自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 部件连接一起使用。 在 Web 部件连接中,驻留在区域中的两个 WebPartZoneBase 服务器控件建立连接并共享数据,其中一个控件充当使用者,另一个控件充当提供程序。 用于在 Web 部件连接中共享数据的机制是接口实例,提供程序通过回调方法为使用者提供服务。 若要建立连接,使用者和提供程序必须使用相同的接口类型来共享数据。 如果使用者无法识别提供程序发送的接口类型,仍可以通过转换器 (对象) WebPartTransformer 连接控件,该对象) 将提供程序发送的接口实例转换为使用者识别的类型。 有关连接的详细信息,请参阅 WebPartConnectionWeb 部件连接概述

接口 IWebPartRow 是 Web 部件控件集随附的提供程序接口,作为基于数据行创建连接的标准接口。 还可以创建自定义接口以与 Web 部件连接一起使用,但在许多数据驱动的 Web 应用程序中,基于通用字段 (创建连接非常有用,有关详细信息,请参阅 IWebPartField 接口) ,表 (,请参阅 IWebPartTable 接口) 或数据源中的行。 在典型的连接中,充当提供程序的 WebPart 控件将实现 IWebPartRow 接口,并通过特殊的回调方法向使用者提供接口的实例。 例如,提供程序可能会为对应于用户信息表中用户的行实现 IWebPartRow 接口。 WebPart另一个充当使用者的控件将定义一个特殊方法来接收接口实例,然后可以提取用户数据,使用它来查找有关该用户帐户的其他信息,并在页面上显示与该用户相关的所有信息。

接口 IWebPartRow 有两个公开的成员。 属性 Schema 返回有关封装在 对象中的数据行的 PropertyDescriptorCollection 架构信息。 方法 GetRowData 声明一个方法,) 调用回调方法时,实现者 ((例如提供程序控件)用于检索接口实例的行数据。

属性

Schema

获取用于在两个 WebPart 控件之间共享数据的数据行的架构信息。

方法

GetRowData(RowCallback)

返回行的数据,接口将使用该行作为两个 WebPart 控件之间的连接基础。

适用于

另请参阅