RowToFieldTransformer 类
定义
通过 IWebPartField 接口,将 Web 部件连接中的数据从实现 IWebPartRow 接口的提供程序转换为客户期望的数据。Transforms data in a Web Parts connection from a provider that implements the IWebPartRow interface to a consumer expecting data through the IWebPartField interface.
public ref class RowToFieldTransformer sealed : System::Web::UI::WebControls::WebParts::WebPartTransformer, System::Web::UI::WebControls::WebParts::IWebPartField
[System.Web.UI.WebControls.WebParts.WebPartTransformer(typeof(System.Web.UI.WebControls.WebParts.IWebPartRow), typeof(System.Web.UI.WebControls.WebParts.IWebPartField))]
public sealed class RowToFieldTransformer : System.Web.UI.WebControls.WebParts.WebPartTransformer, System.Web.UI.WebControls.WebParts.IWebPartField
[<System.Web.UI.WebControls.WebParts.WebPartTransformer(typeof(System.Web.UI.WebControls.WebParts.IWebPartRow), typeof(System.Web.UI.WebControls.WebParts.IWebPartField))>]
type RowToFieldTransformer = class
inherit WebPartTransformer
interface IWebPartField
Public NotInheritable Class RowToFieldTransformer
Inherits WebPartTransformer
Implements IWebPartField
- 继承
- 属性
- 实现
示例
下面的代码示例演示如何使用 RowToFieldTransformer 对象连接具有不兼容连接点的提供者和使用者。The following code example demonstrates how to use a RowToFieldTransformer object to connect a provider and consumer with incompatible connection points. 该示例的第一部分演示了用作提供程序的 Web 部件控件。The first section of the example shows a Web Parts control that serves as a provider. 名为的提供程序类 RowProviderWebPart 提供通过接口的数据 IWebPartRow 。The provider class, named RowProviderWebPart, provides data through the IWebPartRow interface.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Web.UI;
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 row data.
namespace Samples.AspNet.CS.Controls
{
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.DefaultView[0]);
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
'This sample code creates a Web Parts control that acts as a provider of row data.
Namespace Samples.AspNet.VB.Controls
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 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.DefaultView(0))
End Sub
End Class
End Namespace
该示例的第二部分包含一个 Web 部件控件,该控件是 Web 部件连接的使用者。The second section of the example contains a Web Parts control that is a consumer of a Web Parts connection. 名为的使用者类 FieldConsumerWebPart 需要接口中的数据 IWebPartField 。The consumer class, named FieldConsumerWebPart, expects data from the IWebPartField interface.
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;
namespace Samples.AspNet.CS.Controls
{
// This sample code creates a Web Parts control that acts
// as a consumer of an IField interface.
// A consumer WebPart control that consumes strings.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class FieldConsumerWebPart : WebPart
{
private IWebPartField _provider;
private object _fieldValue;
private void GetFieldValue(object fieldValue)
{
_fieldValue = fieldValue;
}
public bool ConnectionPointEnabled
{
get
{
object o = ViewState["ConnectionPointEnabled"];
return (o != null) ? (bool)o : true;
}
set
{
ViewState["ConnectionPointeEnabled"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
if (_provider != null)
{
_provider.GetFieldValue(new FieldCallback(GetFieldValue));
}
base.OnPreRender(e);
}
protected override void RenderContents(HtmlTextWriter writer)
{
if (_provider != null)
{
PropertyDescriptor prop = _provider.Schema;
if (prop != null && _fieldValue != null)
{
writer.Write(prop.DisplayName + ": " + _fieldValue);
}
else
{
writer.Write("No data");
}
}
else
{
writer.Write("Not connected");
}
}
[ConnectionConsumer("Field")]
public void SetConnectionInterface(IWebPartField provider)
{
_provider = provider;
}
private class FieldConsumerConnectionPoint : ConsumerConnectionPoint
{
public FieldConsumerConnectionPoint(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 ((FieldConsumerWebPart)control).ConnectionPointEnabled;
}
}
}
}
Imports System.ComponentModel
Imports System.Reflection
Imports System.Collections
Imports System.Data
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 consumer of an IField interface.
Public Class FieldConsumerWebPart
Inherits WebPart
Private _provider As IWebPartField
Private _fieldValue As Object
Private Sub GetFieldValue(ByVal fieldValue As Object)
_fieldValue = fieldValue
End Sub
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
If Not (_provider Is Nothing) Then
_provider.GetFieldValue((New FieldCallback(AddressOf GetFieldValue)))
End If
MyBase.OnPreRender(e)
End Sub
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
If Not (_provider Is Nothing) Then
Dim prop As PropertyDescriptor = _provider.Schema
If Not (prop Is Nothing) AndAlso Not (_fieldValue Is Nothing) Then
writer.Write(prop.DisplayName & ": " & _fieldValue)
Else
writer.Write("No data")
End If
Else
writer.Write("Not connected")
End If
End Sub
<ConnectionConsumer("Field")> _
Public Sub SetConnectionInterface(ByVal provider As IWebPartField)
_provider = provider
End Sub
Private Class FieldConsumerConnectionPoint
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 Class
该示例的第三部分显示一个页面,其中包含两个控件,并定义 RowToFieldTransformer 用于连接两个控件的对象。The third section of the example shows a page that contains the two controls and defines the RowToFieldTransformer object for connecting the two controls.
<%@ Page Language="C#" %>
<%@ register tagprefix="uc1"
tagname="DisplayModeMenuCS"
src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="wp"
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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager id="manager" runat="server">
<staticconnections>
<asp:WebPartConnection ID="conn1" ProviderID="rp1" ConsumerID="fc1">
<asp:RowToFieldTransformer FieldName="Zip Code"/>
</asp:WebPartConnection>
</staticconnections>
</asp:webpartmanager>
<uc1:displaymodemenucs id="menu1" runat="server" />
<table>
<tr valign="top">
<td>
<asp:webpartzone id="zone1" headertext="zone1" runat="server">
<zonetemplate>
<wp:RowProviderWebPart Title="provider" ID="rp1" runat="server" />
<wp:FieldConsumerWebPart Title="consumer" ID="fc1" runat="server" />
</zonetemplate>
</asp:webpartzone>
</td>
<td>
<asp:connectionszone id="connectionszone1" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ register tagprefix="uc1"
tagname="DisplayModeMenuVB"
src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="wp"
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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager id="manager" runat="server">
<staticconnections>
<asp:WebPartConnection ID="conn1" ProviderID="rp1" ConsumerID="fc1">
<asp:RowToFieldTransformer FieldName="Zip Code"/>
</asp:WebPartConnection>
</staticconnections>
</asp:webpartmanager>
<uc1:displaymodemenuvb id="menu1" runat="server" />
<table>
<tr valign="top">
<td>
<asp:webpartzone id="zone1" headertext="zone1" runat="server">
<zonetemplate>
<wp:RowProviderWebPart Title="provider" ID="rp1" runat="server" />
<wp:FieldConsumerWebPart Title="consumer" ID="fc1" runat="server" />
</zonetemplate>
</asp:webpartzone>
</td>
<td>
<asp:connectionszone id="connectionszone1" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
此代码示例包含一个用户控件,使您可以在 Web 部件页上更改显示模式。The code example includes a user control that enables you to change display modes on a Web Parts page. 该用户控件的源代码来自其他主题。The source code for the user control comes from another topic. 你可以从演练中为用户控件获取 .ascx 文件 :在 Web 部件页上更改显示模式,并且该文件必须位于 .aspx 页面所在的文件夹中。You can obtain the .ascx file for the user control from Walkthrough: Changing Display Modes on a Web Parts Page, and it must be placed in the same folder as the .aspx page.
注解
转换器用于在两个不兼容的连接点之间转换两个 Web 部件控件之间的数据。Transformers are used to translate data between two Web Parts controls with incompatible connection points. RowToFieldTransformer对象将数据从实现接口的提供程序转换 IWebPartRow 为需要接口中的数据的使用者 IWebPartField 。A RowToFieldTransformer object transforms data from a provider implementing the IWebPartRow interface to a consumer requiring data from the IWebPartField interface. RowToFieldTransformer类允许连接到这些不兼容的连接点的控件。The RowToFieldTransformer class allows controls with these incompatible connection points to be connected.
构造函数
| RowToFieldTransformer() |
初始化 RowToFieldTransformer 类的新实例。Initializes a new instance of the RowToFieldTransformer class. |
属性
| FieldName |
获取或设置要转换的值的名称。Gets or sets the name of the value to transform. |
方法
| CreateConfigurationControl() |
在 RowToFieldTransformer 区域中显示配置 ConnectionsZone 转换器的 ASP.NET 控件。Displays an ASP.NET control that configures a RowToFieldTransformer transformer in the ConnectionsZone zone. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| LoadConfigurationState(Object) |
加载通过 SaveConfigurationState() 方法保存的配置状态。Loads the configuration state saved with the SaveConfigurationState() method. (继承自 WebPartTransformer) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| SaveConfigurationState() |
将用户设置的配置状态保存在 ASP.NET 配置控件中。Saves the configuration state set by the user in the ASP.NET configuration control. (继承自 WebPartTransformer) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
| Transform(Object) |
提供用于转换数据的对象。Provides an object for transforming the data. |
显式接口实现
| IWebPartField.GetFieldValue(FieldCallback) |
返回字段的值,该字段由接口用作两个 Web 部件控件之间的连接基础。Returns the value of the field that is being used by the interface as the basis of a connection between two Web Parts controls. |
| IWebPartField.Schema |
获取用于在两个 Web 部件控件之间共享数据的数据字段的架构信息。Gets the schema information for a data field that is used to share data between two Web Parts controls. |