WebPartTransformer 类

定义

为转换器类提供基本实现,以便在两个不兼容的连接点之间转换数据。Provides basic implementation for transformer classes to convert data between two incompatible connection points.

public ref class WebPartTransformer abstract
public abstract class WebPartTransformer
type WebPartTransformer = class
Public MustInherit Class WebPartTransformer
继承
WebPartTransformer
派生

示例

下面的代码示例演示如何创建派生WebPartTransformer自类的自定义转换器。The following code example demonstrates how to create a customized transformer that derives from the WebPartTransformer class. 转换器 (名为RowToStringTransformer) 允许 Web 部件提供商和 Web 部件使用者连接到不兼容的连接点。The transformer, named RowToStringTransformer, allows for a Web Parts provider and Web Parts consumer with incompatible connection points to be connected. 提供程序提供类型IWebPartRow的数据, 但使用者只接受类型String的数据。The provider presents data of type IWebPartRow, but the consumer accepts only data of type String. RowToStringTransformer类执行必要的转换。The RowToStringTransformer class performs the necessary conversion.

此代码示例不包括提供程序或使用者的实现。The code example does not include an implementation of the provider or consumer. 您必须创建一个实现IWebPartRow接口的提供程序和一个使用名IString为的自定义接口的使用者, 这样示例才能正常运行。You must create a provider that implements the IWebPartRow interface and a consumer that expects data through a customized interface named IString for the example to work.

// An interface that the transformer provides to the consumer.
[AspNetHostingPermission(SecurityAction.Demand,
  Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
  Level = AspNetHostingPermissionLevel.Minimal)]
public interface IString
{
    void GetStringValue(StringCallback callback);
}
' An interface that the transformer provides to the consumer.
<AspNetHostingPermission(SecurityAction.Demand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Interface IString
    Sub GetStringValue(ByVal callback As StringCallback)
End Interface

此代码示例的第一部分包含提供程序和使用者的代码 Web 部件控件以及转换器的代码。The first section of the code example contains code for the provider and consumer Web Parts controls, and the code for the transformer.

// A transformer that transforms a row to a string.
[AspNetHostingPermission(SecurityAction.Demand,
  Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
  Level = AspNetHostingPermissionLevel.Minimal)]
[WebPartTransformer(typeof(IWebPartRow), typeof(IString))]
public class RowToStringTransformer : WebPartTransformer, IString
{

    private IWebPartRow _provider;
    private StringCallback _callback;

    private void GetRowData(object rowData)
    {
        PropertyDescriptorCollection props = _provider.Schema;
        if (props != null && props.Count > 0 && rowData != null)
        {
            string returnValue = String.Empty;
            foreach (PropertyDescriptor prop in props)
            {
                if (prop != props[0])
                {
                    returnValue += ", ";
                }
                returnValue += prop.DisplayName + ": " + prop.GetValue(rowData);
            }
            _callback(returnValue);
        }
        else
        {
            _callback(null);
        }
    }
    
    public override object Transform(object providerData)
    {
        _provider = (IWebPartRow)providerData;
        return this;
    }

    void IString.GetStringValue(StringCallback callback)
    {
        if (callback == null)
        {
            throw new ArgumentNullException("callback");
        }

        if (_provider != null)
        {
            _callback = callback;
            _provider.GetRowData(new RowCallback(GetRowData));
        }
        else
        {
            callback(null);
        }
    }
}
' A transformer that transforms a row to a string.
<AspNetHostingPermission(SecurityAction.Demand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
<WebPartTransformer(GetType(IWebPartRow), GetType(IString))> _
Public Class RowToStringTransformer
    Inherits WebPartTransformer
    Implements IString

    Private _provider As IWebPartRow
    Private _callback As StringCallback

    Private Sub GetRowData(ByVal rowData As Object)
        Dim props As PropertyDescriptorCollection = _provider.Schema

        If ((Not (props Is Nothing)) AndAlso (props.Count > 0) _
          AndAlso (Not (rowData Is Nothing))) Then
            Dim returnValue As String = String.Empty
            For Each prop As PropertyDescriptor In props
                If Not (prop Is props(0)) Then
                    returnValue += ", "
                End If
                returnValue += prop.DisplayName.ToString() + ": " + _
                    prop.GetValue(rowData).ToString()
            Next
            _callback(returnValue)
        Else
            _callback(Nothing)
        End If
    End Sub

    Public Overrides Function Transform(ByVal providerData As Object) As Object
        _provider = CType(providerData, IWebPartRow)
        Return Me
    End Function


    Sub GetStringValue(ByVal callback As StringCallback) _
       Implements IString.GetStringValue
        If (callback Is Nothing) Then
            Throw New ArgumentNullException("callback")
        End If

        If (Not (_provider Is Nothing)) Then
            _callback = callback
            _provider.GetRowData(New RowCallback(AddressOf GetRowData))
        Else
            callback(Nothing)
        End If
    End Sub
End Class

此代码示例的第二部分演示如何在WebPartConnection对象的声明性语法中包含转换器。The second section of the code example shows how to include the transformer within the declarative syntax for a WebPartConnection object.

<%@ Page language="c#" trace="false" debug="true" %> 
<%@ register tagprefix="uc1" 
    tagname="DisplayModeMenuCS" 
    src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="wp" 
    NameSpace="Samples.AspNet.CS.Controls" %>

<script runat="server">

</script>

<!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>
<title>Web Parts Transformer Sample Page</title>
</head>
<body>
<form id="Form1" runat="server">

<asp:webpartmanager id="manager" runat="server">
  <staticconnections>
    <asp:webpartconnection id="conn1" providerid="p1" consumerid="c1">
      <wp:rowtostringtransformer />
    </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 id="p1" runat="server" />
      <wp:stringconsumerwebpart id="c1" 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" %>

<script runat="server">

</script>

<!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>
    <title>Web Parts Transformers Sample Page</title>
</head>
<body>
<form id="Form1" runat="server">

<asp:webpartmanager id="manager" runat="server">
  <staticconnections>
    <asp:webpartconnection id="conn1" providerid="p1" consumerid="c1">
      <wp:rowtostringtransformer />
    </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 id="p1" runat="server" />
      <wp:stringconsumerwebpart id="c1" runat="server" />
    </zonetemplate>
  </asp:webpartzone>
  </td>
  <td>
  <asp:connectionszone id="connectionszone1" runat="server" />
  </td>
</tr>
</table>

</form>
</body>
</html>

必须在 web.config 文件的<transformers>节中指定自定义的转换器, 以便在网页中使用。A customized transformer must be specified in the <transformers> section of the Web.config file to be available for use in a Web page. 此代码示例的第三部分演示如何将自定义转换器添加到 web.config 文件。The third section of the code example shows how to add the customized transformer to the Web.config file.

<webParts enableExport="true">  
    <transformers>  
       <add name="RowToStringTransformer"  
          type="Samples.AspNet.VB.Controls.RowToStringTransformer" />  
    </transformers>  
</webParts>  
<webParts enableExport="true">  
    <transformers>  
       <add name="RowToStringTransformer"  
          type="Samples.AspNet.CS.Controls.RowToStringTransformer" />  
    </transformers>  
</webParts>  

此代码示例包含一个用户控件, 使您可以在 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. 连接点在通过不同的接口提供或使用数据时不兼容。Connection points are incompatible when they provide or consume data through different interfaces. 例如, 实现类型IWebPartRow为的提供者连接点的提供程序无法直接连接到应为类型IWebPartTable的提供者连接点的使用者。For example, a provider implementing a provider connection point of type IWebPartRow could not directly connect to a consumer expecting a provider connection point of type IWebPartTable. 相反, 必须使用转换器来连接两个 Web 部件控件。Instead, a transformer must be used to connect the two Web Parts controls.

转换器接受提供程序连接点支持的类型的数据。The transformer accepts data of the type supported by the provider connection point. 它执行必要的内部处理, 将该数据转换为使用者连接点支持的类型。It does the necessary internal processing to convert that data into the type supported by the consumer connection point.

转换器可以提供用户界面 (UI), 以允许用户在连接模式下配置转换器。A transformer can provide a user interface (UI) that allows the user to configure the transformer when in the connect mode. 配置控件通过CreateConfigurationControl方法检索, 并显示在 Web 部件连接区域中。The configuration control is retrieved through the CreateConfigurationControl method and is displayed in a Web Parts connections zone.

WebPartTransformer是一个抽象类, 必须进行扩展, 以便在不同类型的连接点之间提供自定义的转换。WebPartTransformer is an abstract class and must be extended to provide customized translations between different types of connection points.

继承者说明

必须重写Transform(Object)方法。You must override the Transform(Object) method.

构造函数

WebPartTransformer()

初始化 WebPartTransformer 类的新实例。Initializes a new instance of the WebPartTransformer class.

方法

CreateConfigurationControl()

ConnectionsZone 区域中显示配置转换器的 ASP.NET 控件。Displays an ASP.NET control that configures a 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()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
LoadConfigurationState(Object)

加载通过 SaveConfigurationState() 方法保存的配置状态。Loads the configuration state saved with the SaveConfigurationState() method.

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.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
Transform(Object)

实现后,提供一个用于转换数据的对象。When implemented, provides an object for transforming the data.

适用于