共用方式為


RowToFieldTransformer 類別

定義

將 Web 組件連接的資料,從實作 IWebPartRow 介面的提供者轉換成透過 IWebPartField 介面接收資料的取用者。

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
屬性
實作

範例

下列程式碼範例示範如何使用 物件來連接提供者和取用 RowToFieldTransformer 者與不相容的連接點。 此範例的第一個區段會顯示做為提供者的 Web 元件控制項。 名為 RowProviderWebPart 的提供者類別會透過 IWebPartRow 介面提供資料。

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

此範例的第二個區段包含網頁元件控制項,該控制項是網頁元件連線的取用者。 名為 FieldConsumerWebPart 的取用者類別需要來自 IWebPartField 介面的資料。

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 連接兩個控制項的物件。

<%@ 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>

程式碼範例包含使用者控制項,可讓您變更網頁元件頁面上的顯示模式。 使用者控制項的原始程式碼來自另一個主題。 您可以從逐步解說 :變更網頁元件頁面上的顯示模式,取得使用者控制項的 .ascx 檔案,而且它必須放在與 .aspx 頁面相同的資料夾中。

備註

轉換器可用來轉譯兩個 Web 元件控制項與不相容連接點之間的資料。 RowToFieldTransformer物件會從實作 介面的提供者將資料 IWebPartRow 轉換成需要來自 IWebPartField 介面資料的取用者。 類別 RowToFieldTransformer 可讓具有這些不相容連接點的控制項連線。

建構函式

RowToFieldTransformer()

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

屬性

FieldName

取得或設定要轉換之值的名稱。

方法

CreateConfigurationControl()

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

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
LoadConfigurationState(Object)

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

(繼承來源 WebPartTransformer)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SaveConfigurationState()

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

(繼承來源 WebPartTransformer)
ToString()

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

(繼承來源 Object)
Transform(Object)

提供物件以轉換資料。

明確介面實作

IWebPartField.GetFieldValue(FieldCallback)

傳回欄位的值,介面正在將此欄位用作兩個 Web 組件控制項之間連接的基礎。

IWebPartField.Schema

取得資料欄位的結構描述資訊,這個資料欄位是用於兩個 Web 組件控制項之間共用資料。

適用於