DataBoundControlAdapter クラス
アセンブリ: System.Web (system.web.dll 内)
構文
DataBoundControl クラスから派生したコントロールは、データ ソースにバインドされ、バインド先のデータ ソース内の項目を列挙することによって、そのコントロールのユーザー インターフェイスまたは子コントロール階層を生成します。DataBoundControl は、データ ソースにバインドできるすべてのコントロール (DataGrid コントロールや ListBox コントロールなど) の共通特性を定義する抽象基本クラスです。詳細については、「DataBoundControl」を参照してください。
DataBoundControlAdapter は、特定のブラウザまたはブラウザのクラスに対する DataBoundControl の動作を変更します。また、一部の機能では、フィルタとして機能します。表示動作の適応性の多くは、HtmlTextWriter クラスから派生した特定のクラスにカプセル化できます。したがって、単一のアダプタをブラウザ クラスの複数の動作に使用することができ、また、HtmlTextWriter クラスに順応性を持たせることによってコントロール アダプタを使用する必要がなくなるとも言えます。
.browser 定義ファイルに <controlAdapter> というエントリが存在する場合、各コントロールは、これらのファイルを通じて明示的にアダプタに対応付けられます。そのため、アダプタとコントロールの対応付けに必要なルックアップを実行する場合、DataBoundControl の Adapter プロパティに対するすべてのアクセスには、.browser 定義ファイルから抽出された HttpBrowserCapabilities オブジェクトが使用されます。
処理中に、.NET Framework が、ブラウザ固有のコントロールのメソッドに対する呼び出しを受け取ります。コントロール アダプタが割り当てられている場合、.NET Framework は、関連付けられているアダプタ メソッドを呼び出します。詳細については、「ControlAdapter」を参照してください。
M:System.Web.UI.WebControls.Adapters.DataBoundControlAdapter.PerformDataBinding(System.Collections.IEnumerable) メソッドは、列挙可能なコレクションを、関連付けられている DataBoundControl にバインドします。Control プロパティは、DataBoundControl コントロールへの、厳密に型指定されている参照を返します。
2 つの派生コントロールを作成および使用する例を次に示します。
-
DataBoundControl から派生した MyDataBound クラスは、単純な読み取り専用のグリッド コントロールです。
-
DataBoundControlAdapter から MyDataBoundAdapter クラスは、グリッド データを、小画面のブラウザに適した、行区切り記号付きの 1 次元リストとして表示します。
最初のコード例は、Web ページを使用して、MyDataBound コントロール、および DataView オブジェクトの形式でデータを提供する ObjectDataSource のインスタンスを宣言しています。
2 番目のコード例には、派生した MyDataBound クラスと MyDataBoundAdapter クラスが含まれています。
-
MyDataBound クラスは、PerformDataBinding メソッドをオーバーライドしてデータ ソースの IEnumerator コレクションを保存し、RenderContents メソッドをオーバーライドしてデータ ソースを HTML の <table> として表示します。
-
MyDataBoundAdapter クラスは、PerformDataBinding をオーバーライドして、データ ソースを 1 次元の ArrayList に保存し、行区切り記号を追加します。このクラスは、RenderContents をオーバーライドして、ArrayList を <br /> タグで区切られたフィールドのリストとして表示します。
Imports System Imports System.Data Imports System.Web Imports System.Web.UI Imports System.Collections Imports System.Security Imports System.Security.Permissions Namespace MyControls ' MyDataBound control is a simple read-only grid control. <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class MyDataBound Inherits System.Web.UI.WebControls.DataBoundControl ' This is an enumerator for the data source. Private dataSourceEnumerator As IEnumerator = Nothing ' Render the data source as a table, without row and column headers. Protected Overrides Sub RenderContents( _ ByVal writer As System.Web.UI.HtmlTextWriter) ' Render the <table> tag. writer.RenderBeginTag(HtmlTextWriterTag.Table) ' Render the table rows. While dataSourceEnumerator.MoveNext() ' Get the next data row as an object array. Dim dataArray As Object() = CType( _ dataSourceEnumerator.Current, DataRowView).Row.ItemArray ' Render the <tr> tag. writer.RenderBeginTag(HtmlTextWriterTag.Tr) ' Render the fields of the row. Dim col As Integer For col = 0 To (dataArray.GetLength(0)) - 1 'Render the <td> tag, the field data and the </td> tag. writer.RenderBeginTag(HtmlTextWriterTag.Td) writer.Write(dataArray(col)) writer.RenderEndTag() Next col ' Render the </tr> tag. writer.RenderEndTag() End While ' Render the </table> tag. writer.RenderEndTag() End Sub 'RenderContents ' Data binding consists of saving an enumerator for the data. Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable) dataSourceEnumerator = data.GetEnumerator() End Sub 'PerformDataBinding End Class 'MyDataBound ' MyDataBoundAdapter modifies a MyDataBound control to display a ' grid as a list with row separators. <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class MyDataBoundAdapter Inherits System.Web.UI.WebControls.Adapters.DataBoundControlAdapter ' Returns a strongly-typed reference to the MyDataBound control. Public Shadows ReadOnly Property Control() As MyDataBound Get Return CType(MyBase.Control, MyDataBound) End Get End Property ' One-dimensional list for the grid data. Private dataArray As New ArrayList() ' Copy grid data to one-dimensional list, add row separators. Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable) Dim dataSourceEnumerator As IEnumerator = data.GetEnumerator() ' Iterate through the table rows. While dataSourceEnumerator.MoveNext() ' Add the next data row to the ArrayList. dataArray.AddRange(CType(dataSourceEnumerator.Current, _ DataRowView).Row.ItemArray) ' Add a separator to the ArrayList. dataArray.Add("----------") End While End Sub 'PerformDataBinding ' Render the data source as a one-dimensional list. Protected Overrides Sub RenderContents( _ ByVal writer As System.Web.UI.HtmlTextWriter) ' Render the data list. Dim col As Integer For col = 0 To dataArray.Count - 1 writer.Write(dataArray(col)) writer.WriteBreak() Next col End Sub 'RenderContents End Class 'MyDataBoundAdapter End Namespace ' MyControls
using System; using System.Data; using System.Web; using System.Web.UI; using System.Collections; using System.Security.Permissions; namespace MyControls { // MyDataBound control is a simple read-only grid control. [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class MyDataBound : System.Web.UI.WebControls.DataBoundControl { // This is an enumerator for the data source. IEnumerator dataSourceEnumerator = null; // Render the data source as a table, without row and column headers. protected override void RenderContents( System.Web.UI.HtmlTextWriter writer) { // Render the <table> tag. writer.RenderBeginTag(HtmlTextWriterTag.Table); // Render the table rows. while (dataSourceEnumerator.MoveNext()) { // Get the next data row as an object array. object[] dataArray = ((DataRowView)dataSourceEnumerator.Current).Row.ItemArray; // Render the <tr> tag. writer.RenderBeginTag(HtmlTextWriterTag.Tr); // Render the fields of the row. for(int col = 0; col<dataArray.GetLength(0) ; col++) { //Render the <td> tag, the field data and the </td> tag. writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write(dataArray[col]); writer.RenderEndTag(); } // Render the </tr> tag. writer.RenderEndTag(); } // Render the </table> tag. writer.RenderEndTag(); } // Data binding consists of saving an enumerator for the data. protected override void PerformDataBinding(IEnumerable data) { dataSourceEnumerator = data.GetEnumerator(); } } // MyDataBoundAdapter modifies a MyDataBound control to display a // grid as a list with row separators. [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class MyDataBoundAdapter : System.Web.UI.WebControls.Adapters.DataBoundControlAdapter { // Returns a strongly-typed reference to the MyDataBound control. public new MyDataBound Control { get { return (MyDataBound)base.Control; } } // One-dimensional list for the grid data. ArrayList dataArray = new ArrayList(); // Copy grid data to one-dimensional list, add row separators. protected override void PerformDataBinding(IEnumerable data) { IEnumerator dataSourceEnumerator = data.GetEnumerator(); // Iterate through the table rows. while (dataSourceEnumerator.MoveNext()) { // Add the next data row to the ArrayList. dataArray.AddRange( ((DataRowView)dataSourceEnumerator.Current).Row.ItemArray); // Add a separator to the ArrayList. dataArray.Add("----------"); } } // Render the data source as a one-dimensional list. protected override void RenderContents( System.Web.UI.HtmlTextWriter writer) { // Render the data list. for( int col=0; col<dataArray.Count;col++) { writer.Write(dataArray[col]); writer.WriteBreak(); } } } }
3 番目のコード例では、構成ファイルを使用して、Microsoft Internet Explorer ブラウザ用の MyDataBound コントロールにはコントロール アダプタを使用せず、Openwave UP ブラウザ用の MyDataBound コントロールには MyDataBoundAdapter を使用するように指定しています。
System.Web.UI.Adapters.ControlAdapter
System.Web.UI.WebControls.Adapters.WebControlAdapter
System.Web.UI.WebControls.Adapters.DataBoundControlAdapter
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- DataBoundControlAdapter クラスのページへのリンク