WebPartTransformer Class

Definition

Provides basic implementation for transformer classes to convert data between two incompatible connection points.

C#
public abstract class WebPartTransformer
Inheritance
WebPartTransformer
Derived

Examples

The following code example demonstrates how to create a customized transformer that derives from the WebPartTransformer class. The transformer, named RowToStringTransformer, allows for a Web Parts provider and Web Parts consumer with incompatible connection points to be connected. The provider presents data of type IWebPartRow, but the consumer accepts only data of type String. The RowToStringTransformer class performs the necessary conversion.

The code example does not include an implementation of the provider or consumer. 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.

C#
// 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);
}

The first section of the code example contains code for the provider and consumer Web Parts controls, and the code for the transformer.

C#
// 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);
        }
    }
}

The second section of the code example shows how to include the transformer within the declarative syntax for a WebPartConnection object.

ASP.NET (C#)
<%@ 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>

A customized transformer must be specified in the <transformers> section of the Web.config file to be available for use in a Web page. The third section of the code example shows how to add the customized transformer to the Web.config file.

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

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. 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.

Remarks

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. 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. 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.

A transformer can provide a user interface (UI) that allows the user to configure the transformer when in the connect mode. The configuration control is retrieved through the CreateConfigurationControl method and is displayed in a Web Parts connections zone.

WebPartTransformer is an abstract class and must be extended to provide customized translations between different types of connection points.

Notes to Implementers

You must override the Transform(Object) method.

Constructors

WebPartTransformer()

Initializes a new instance of the WebPartTransformer class.

Methods

CreateConfigurationControl()

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.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
LoadConfigurationState(Object)

Loads the configuration state saved with the SaveConfigurationState() method.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SaveConfigurationState()

Saves the configuration state set by the user in the ASP.NET configuration control.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
Transform(Object)

When implemented, provides an object for transforming the data.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1