WebPartTransformer 類別

定義

提供轉換程式類別的基本實作,以便在兩個不相容連接點之間轉換資料。

public ref class WebPartTransformer abstract
public abstract class WebPartTransformer
type WebPartTransformer = class
Public MustInherit Class WebPartTransformer
繼承
WebPartTransformer
衍生

範例

下列程式碼範例示範如何建立衍生自 類別的 WebPartTransformer 自訂轉換器。 名為 RowToStringTransformer 的轉換器允許Web 組件提供者,並Web 組件取用者連接不相容的連接點。 提供者會呈現 類型的 IWebPartRow 資料,但取用者只接受 類型 String 的資料。 類別 RowToStringTransformer 會執行必要的轉換。

程式碼範例不包含提供者或取用者的實作。 您必須建立實作 介面的 IWebPartRow 提供者,以及透過名為 IString 的自訂介面預期資料的取用者,範例才能運作。

// 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 組件控制項的程式碼,以及轉換器的程式碼。

// 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 中包含轉換器。

<%@ 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> ,才能在網頁中使用。 程式碼範例的第三個區段示範如何將自訂的轉換器新增至Web.config檔案。

<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 組件頁面上的顯示模式。 使用者控制項的原始程式碼來自另一個主題。 您可以從逐步解說:變更Web 組件頁面上的顯示模式,取得使用者控制項的 .ascx 檔案,而且它必須放在與 .aspx 頁面相同的資料夾中。

備註

轉換器可用來轉譯兩個Web 組件控制項與不相容連接點之間的資料。 連接點透過不同的介面提供或取用資料時不相容。 例如,實作 型別 IWebPartRow 提供者連接點的提供者無法直接連接到預期類型 IWebPartTable 提供者連接點的取用者。 相反地,轉換程式必須用來連接兩個Web 組件控制項。

轉換器接受提供者連接點所支援之型別的資料。 它會執行必要的內部處理,將該資料轉換成取用者連接點所支援的類型。

轉換器可以提供使用者介面 (UI) ,讓使用者在連接模式中設定轉換器。 組態控制項是透過 CreateConfigurationControl 方法來擷取,並顯示在Web 組件連線區域中。

WebPartTransformer 是抽象類別,必須擴充以提供不同連接點類型之間的自訂翻譯。

給實施者的注意事項

您必須覆寫 Transform(Object) 方法。

建構函式

WebPartTransformer()

初始化 WebPartTransformer 類別的新執行個體。

方法

CreateConfigurationControl()

顯示在 ConnectionsZone 區域中設定轉換程式的 ASP.NET 控制項。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
LoadConfigurationState(Object)

載入以 SaveConfigurationState() 方法所儲存的組態狀態。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SaveConfigurationState()

儲存使用者在 ASP.NET 組態控制項中所設定的組態狀態。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
Transform(Object)

實作時,提供物件以轉換資料。

適用於