IWebPartTable 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用整个数据表定义一个用于连接两个服务器控件的提供者接口。
public interface class IWebPartTable
public interface IWebPartTable
type IWebPartTable = interface
Public Interface IWebPartTable
示例
下面的代码示例演示如何使用 IWebPartTable 接口在两个控件之间创建静态连接。 该代码示例包含三个部分:
可以使用 接口形成连接的两个自定义 WebPart 控件的 IWebPartTable 源代码,其中一个控件充当提供程序,另一个控件充当使用者。
承载控件的网页,以持久性格式声明静态连接。
说明运行示例代码时发生的情况。
代码示例的第一部分是两个自定义控件的源代码。 第一个是提供程序的代码,该提供程序实现 IWebPartTable 接口。 为简单起见,在示例中,提供程序创建包含一些数据的表,而不是连接到数据库。 方法 GetConnectionInterface 充当提供程序的连接点,即将接口实例返回给使用者的回调方法。 对于使用者,它在其名为 SetConnectionInterface的 方法中从提供程序中检索接口实例,该方法用 ConnectionConsumer 特性标记。 检索接口实例后,使用者在其 OnPreRender 方法中调用 提供程序中 方法的 GetTableData 实现,以检索实际数据并将其写入页面。
若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 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 table data.
[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 creates a Web Parts control that acts as a consumer
// of information provided by the TableProvider.ascx control.
[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)
{
}
} // TableConsumerConnectionPoint
} // TableConsumer
} // 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 table data.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class TableProviderWebPart
Inherits WebPart
Implements IWebPartTable
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
Public ReadOnly Property Schema() As _
ComponentModel.PropertyDescriptorCollection Implements IWebPartTable.Schema
Get
Return TypeDescriptor.GetProperties(_table.DefaultView(0))
End Get
End Property
Public Sub GetTableData(ByVal callback As TableCallback) _
Implements IWebPartTable.GetTableData
callback(_table.Rows)
End Sub
Public Property ConnectionPointEnabled() As Boolean
Get
Dim o As Object = ViewState("ConnectionPointEnabled")
Return IIf(Not (o Is Nothing), CBool(o), True)
End Get
Set(ByVal value As Boolean)
ViewState("ConnectionPointEnabled") = value
End Set
End Property
<ConnectionProvider("Table", GetType(TableProviderConnectionPoint), _
AllowsMultipleConnections:=True)> _
Public Function GetConnectionInterface() As IWebPartTable
Return New TableProviderWebPart()
End Function
End Class
' The connection point for the provider control.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class TableProviderConnectionPoint
Inherits ProviderConnectionPoint
Public Sub New(ByVal callbackMethod As MethodInfo, _
ByVal interfaceType As Type, ByVal controlType As Type, _
ByVal name As String, ByVal id As String, _
ByVal allowsMultipleConnections As Boolean)
MyBase.New(callbackMethod, interfaceType, controlType, _
name, id, allowsMultipleConnections)
End Sub
Public Overrides Function GetEnabled(ByVal control _
As Control) As Boolean
Return CType(control, TableProviderWebPart).ConnectionPointEnabled
End Function
End Class
' This code sample creates a Web Parts control that acts as a consumer
' of information provided by the TableProvider.ascx control.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class TableConsumer
Inherits WebPart
Private _provider As IWebPartTable
Private _tableData As ICollection
Private Sub GetTableData(ByVal tableData As ICollection)
_tableData = CType(tableData, ICollection)
End Sub
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
If Not (_provider Is Nothing) Then
_provider.GetTableData(New TableCallback(AddressOf GetTableData))
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))
Next o
writer.WriteBreak()
writer.WriteLine()
count = count + 1
Next prop
Else
writer.Write("No data")
End If
Else
writer.Write("Not connected")
End If
End Sub
<ConnectionConsumer("Table")> _
Public Sub SetConnectionInterface(ByVal provider As IWebPartTable)
_provider = provider
End Sub
End Class
' The connection point for the consumer control.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class TableConsumerConnectionPoint
Inherits ConsumerConnectionPoint
Public Sub New(ByVal callbackMethod As MethodInfo, _
ByVal interfaceType As Type, ByVal controlType As Type, _
ByVal name As String, ByVal id As String, _
ByVal allowsMultipleConnections As Boolean)
MyBase.New(callbackMethod, interfaceType, controlType, name, _
id, allowsMultipleConnections)
End Sub
End Class
End Namespace ' Samples.AspNet.CS.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>
在浏览器中加载页面。 使用者控件显示从指定表提供的数据,提供程序通过 接口的 IWebPartTable 实例提供这些数据。
注解
此接口旨在与 Web 部件连接一起使用。 在 Web 部件连接中,驻留在区域中的两个 WebPartZoneBase 服务器控件建立连接并共享数据,其中一个控件充当使用者,另一个控件充当提供程序。 在 Web 部件连接中共享数据的机制是接口实例,提供程序通过回调方法为使用者提供服务。 若要建立连接,使用者和提供程序必须使用相同的接口类型来共享数据。 如果使用者无法识别提供程序发送的接口类型,则仍可以通过转换器 (WebPartTransformer 对象) 连接控件,该对象) 将提供程序发送的接口实例转换为使用者识别的类型。 有关连接的详细信息,请参阅 WebPartConnection 和 Web 部件连接概述。
接口 IWebPartTable 是 Web 部件控件集随附的提供程序接口,作为基于数据表创建连接的标准接口。 还可以创建自定义接口以用于 Web 部件连接,但在许多数据驱动的 Web 应用程序中,基于公共字段创建连接非常有用 (了解详细信息,请参阅 IWebPartField 接口) ,行 (了解详细信息,请参阅 IWebPartRow 接口) ,或数据源中的表。 在典型的连接中,充当提供程序的 WebPart 控件将实现 IWebPartTable 接口,并在特殊的回调方法中向使用者提供接口的实例。 例如,提供程序可能会为包含财务绩效数据的表实现 IWebPartTable 接口。 另 WebPart 一个充当使用者的控件将定义用于接收接口实例的特殊方法,然后可以提取数据并呈现图表以显示结果信息。
接口 IWebPartTable 有两个公开的成员。 属性 Schema 返回有关封装在 对象中的数据表的 PropertyDescriptorCollection 架构信息。 方法 GetTableData 声明一个方法,实现者 (如提供程序控件) 调用回调方法时用于检索接口实例的表数据。
属性
| Schema |
获取用于在两个 WebPart 控件之间共享数据的数据表的架构信息。 |
方法
| GetTableData(TableCallback) |
返回表的数据,该表正由接口用作两个 WebPart 控件之间的连接基础。 |