DataBoundControl イベント

名前 | 説明 | |
---|---|---|
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。 ( Control から継承されます。) |
![]() | DataBound | サーバー コントロールがデータ ソースにバインドした後に発生します。 ( BaseDataBoundControl から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。 ( Control から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。 ( Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。 ( Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。 ( Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。 ( Control から継承されます。) |

DataBoundControl クラス
アセンブリ: System.Web (system.web.dll 内)


DataBoundControl クラスは、ASP.NET コントロールに使用される基本クラスです。このコントロールは、ASP.NET データ ソース コントロールから表形式または一覧形式のデータを取得し、コントロールのユーザー インターフェイス (UI) 要素をそのデータにバインドして表示します。GridView、DetailsView、FormView などの複合データ バインド コントロール、BulletedList、CheckBoxList などの一覧形式のデータ バインド コントロール、AdRotator などのその他のコントロールは、DataBoundControl から派生します。
ページ開発者は、DataBoundControl クラスを直接使用せずに、このクラスから派生するコントロールを使用します。
コントロール開発者はこのクラスを拡張して、IDataSource インターフェイスを実装するクラス、および DataSourceControl クラスと DataSourceView クラスから派生するクラスで機能するデータ バインド コントロールを作成します。DataBoundControl クラスから派生クラスを作成する場合、PerformDataBinding メソッドをオーバーライドして、コントロールの UI 要素を GetData メソッドで取得したデータにバインドします。ほとんどの場合、派生クラスでオーバーライドするメソッドは、PerformDataBinding メソッドだけです。
ASP.NET 2.0 データ バインド コントロールの場合、PerformSelect メソッドは DataBind メソッドと等価で、実行時にデータをバインドするために呼び出されます。PerformSelect メソッドは、GetData メソッドと PerformDataBinding メソッドを呼び出します。

DataBoundControl クラスから派生クラスを作成して、カスタムのデータ バインド コントロールを作成する方法を次のコード例に示します。TextBoxSet コントロールは、関連するデータ ソース コントロールから取得した各データ項目に対して TextBox コントロールを作成し、実行時にデータ項目の値にバインドします。Render メソッドの現在の実装では、TextBox コントロールを順序なしの一覧として描画します。
Imports System Imports System.Collections Imports System.ComponentModel Imports System.Security.Permissions Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.Controls.VB <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal), _ AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class TextBoxSet Inherits DataBoundControl Private alBoxSet As IList Public ReadOnly Property BoxSet() As IList Get If alBoxSet Is Nothing Then alBoxSet = New ArrayList() End If Return alBoxSet End Get End Property Public Property DataTextField() As String Get Dim o As Object = ViewState("DataTextField") If o Is Nothing Then Return String.Empty Else Return CStr(o) End If End Get Set(ByVal value As String) ViewState("DataTextField") = value If (Initialized) Then OnDataPropertyChanged() End If End Set End Property Protected Overrides Sub PerformSelect() ' Call OnDataBinding here if bound to a data source using the ' DataSource property (instead of a DataSourceID) because the ' data-binding statement is evaluated before the call to GetData. If Not IsBoundUsingDataSourceID Then OnDataBinding(EventArgs.Empty) End If ' The GetData method retrieves the DataSourceView object from the ' IDataSource associated with the data-bound control. GetData().Select(CreateDataSourceSelectArguments(), _ AddressOf OnDataSourceViewSelectCallback) ' The PerformDataBinding method has completed. RequiresDataBinding = False MarkAsDataBound() ' Raise the DataBound event. OnDataBound(EventArgs.Empty) End Sub 'PerformSelect Private Sub OnDataSourceViewSelectCallback(ByVal retrievedData As IEnumerable) ' Call OnDataBinding only if it has not already ' been called in the PerformSelect method. If IsBoundUsingDataSourceID Then OnDataBinding(EventArgs.Empty) End If ' The PerformDataBinding method binds the data in the retrievedData ' collection to elements of the data-bound control. PerformDataBinding(retrievedData) End Sub 'OnDataSourceViewSelectCallback Protected Overrides Sub PerformDataBinding(ByVal retrievedData As IEnumerable) MyBase.PerformDataBinding(retrievedData) ' If the data is retrieved from an IDataSource as an IEnumerable ' collection, attempt to bind its values to a set of TextBox controls. If Not (retrievedData Is Nothing) Then Dim dataItem As Object For Each dataItem In retrievedData Dim box As New TextBox() ' The dataItem is not just a string, but potentially ' a System.Data.DataRowView or some other container. ' If DataTextField is set, use it to determine which ' field to render. Otherwise, use the first field. If DataTextField.Length > 0 Then box.Text = DataBinder.GetPropertyValue( _ dataItem, DataTextField, Nothing) Else Dim props As PropertyDescriptorCollection = _ TypeDescriptor.GetProperties(dataItem) ' Set the "default" value of the TextBox. box.Text = String.Empty ' Set the true data-bound value of the TextBox, ' if possible. If props.Count >= 1 Then If props(0).GetValue(dataItem) IsNot Nothing Then box.Text = props(0).GetValue(dataItem).ToString() End If End If End If BoxSet.Add(box) Next dataItem End If End Sub 'PerformDataBinding Protected Overrides Sub Render(ByVal writer As HtmlTextWriter) ' Render nothing if the control is empty. If BoxSet.Count <= 0 Then Return End If ' Make sure the control is declared in a form tag with runat=server. If Not (Page Is Nothing) Then Page.VerifyRenderingInServerForm(Me) End If ' For this example, render the BoxSet as ' an unordered list of TextBox controls. writer.RenderBeginTag(HtmlTextWriterTag.Ul) Dim item As Object For Each item In BoxSet Dim box As TextBox = CType(item, TextBox) ' Write each element as ' <li><input type="text" value="string"><input/></li> writer.WriteBeginTag("li") writer.Write(">") writer.WriteBeginTag("input") writer.WriteAttribute("type", "text") writer.WriteAttribute("value", box.Text) writer.Write(">") writer.WriteEndTag("input") writer.WriteEndTag("li") Next item writer.RenderEndTag() End Sub 'Render End Namespace
using System; using System.Collections; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Samples.AspNet.Controls.CS { [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] public class TextBoxSet : DataBoundControl { private IList alBoxSet; public IList BoxSet { get { if (null == alBoxSet) { alBoxSet = new ArrayList(); } return alBoxSet; } } public string DataTextField { get { object o = ViewState["DataTextField"]; return((o == null) ? string.Empty : (string)o); } set { ViewState["DataTextField"] = value; if (Initialized) { OnDataPropertyChanged(); } } } protected override void PerformSelect() { // Call OnDataBinding here if bound to a data source using the // DataSource property (instead of a DataSourceID), because the // databinding statement is evaluated before the call to GetData. if (! IsBoundUsingDataSourceID) { OnDataBinding(EventArgs.Empty); } // The GetData method retrieves the DataSourceView object from // the IDataSource associated with the data-bound control. GetData().Select(CreateDataSourceSelectArguments(), OnDataSourceViewSelectCallback); // The PerformDataBinding method has completed. RequiresDataBinding = false; MarkAsDataBound(); // Raise the DataBound event. OnDataBound(EventArgs.Empty); } private void OnDataSourceViewSelectCallback(IEnumerable retrievedData) { // Call OnDataBinding only if it has not already been // called in the PerformSelect method. if (IsBoundUsingDataSourceID) { OnDataBinding(EventArgs.Empty); } // The PerformDataBinding method binds the data in the // retrievedData collection to elements of the data-bound control. PerformDataBinding(retrievedData); } protected override void PerformDataBinding(IEnumerable retrievedData) { base.PerformDataBinding(retrievedData); // If the data is retrieved from an IDataSource as an // IEnumerable collection, attempt to bind its values to a // set of TextBox controls. if (retrievedData != null) { foreach (object dataItem in retrievedData) { TextBox box = new TextBox(); // The dataItem is not just a string, but potentially // a System.Data.DataRowView or some other container. // If DataTextField is set, use it to determine which // field to render. Otherwise, use the first field. if (DataTextField.Length > 0) { box.Text = DataBinder.GetPropertyValue(dataItem, DataTextField, null); } else { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(dataItem); // Set the "default" value of the TextBox. box.Text = String.Empty; // Set the true data-bound value of the TextBox , // if possible. if (props.Count >= 1) { if (null != props[0].GetValue(dataItem)) { box.Text = props[0].GetValue(dataItem).ToString(); } } } BoxSet.Add(box); } } } protected override void Render(HtmlTextWriter writer) { // Render nothing if the control is empty. if (BoxSet.Count <= 0) { return; } // Make sure the control is declared in a form tag // with runat=server. if (Page != null) { Page.VerifyRenderingInServerForm(this); } // For this example, render the BoxSet as // an unordered list of TextBox controls. writer.RenderBeginTag(HtmlTextWriterTag.Ul); foreach (object item in BoxSet) { TextBox box = (TextBox) item; // Write each element as // <li><input type="text" value="string"><input/></li> writer.WriteBeginTag("li"); writer.Write(">"); writer.WriteBeginTag("input"); writer.WriteAttribute("type", "text"); writer.WriteAttribute("value", box.Text); writer.Write(">"); writer.WriteEndTag("input"); writer.WriteEndTag("li"); } writer.RenderEndTag(); } } }
前の例で定義した TextBoxSet コントロールを使用して、AccessDataSource コントロールにバインドする方法を次のコード例に示します。
<%@Page language="VB" %> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.VB" Assembly="Samples.AspNet.Controls.VB" %> <html> <head> <title>TextBoxSet Data-Bound Control - VB Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <aspSample:textboxset id="TextBoxSet1" runat="server" datasourceid="AccessDataSource1" /> <asp:accessdatasource id="AccessDataSource1" runat="server" datafile="Northwind.mdb" selectcommand="SELECT lastname FROM Employees" /> </form> </body> </html>
<%@Page language="c#" %> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.CS" Assembly="Samples.AspNet.Controls.CS" %> <html> <head> <title>TextBoxSet Data-Bound Control - C# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <aspSample:textboxset id="TextBoxSet1" runat="server" datasourceid="AccessDataSource1" /> <asp:accessdatasource id="AccessDataSource1" runat="server" datafile="Northwind.mdb" selectcommand="SELECT lastname FROM Employees" /> </form> </body> </html>


System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.DataBoundControl
System.Web.UI.WebControls.AdRotator
System.Web.UI.WebControls.CompositeDataBoundControl
System.Web.UI.WebControls.ListControl


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataBoundControl コンストラクタ
アセンブリ: System.Web (system.web.dll 内)


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataBoundControl プロパティ



DataBoundControl メソッド



DataBoundControl メンバ
データを一覧または表形式で表示するすべての ASP.NET Version 2.0 データ バインド コントロールに対し、基本クラスとして機能します。
DataBoundControl データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | DataBoundControl | 継承クラス インスタンスによって使用される DataBoundControl クラスを初期化します。このコンストラクタは、継承クラスによってのみ呼び出すことができます。 |





名前 | 説明 | |
---|---|---|
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。(Control から継承されます。) |
![]() | DataBound | サーバー コントロールがデータ ソースにバインドした後に発生します。(BaseDataBoundControl から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。(Control から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。(Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。(Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。(Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。(Control から継承されます。) |

- DataBoundControlのページへのリンク