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


DataControlField クラスは、すべての種類のデータ コントロール フィールドの基本クラスとして機能します。データ コントロール フィールドは、DataGridColumn オブジェクトが DataGrid コントロールの列の種類を表すのと同様に、データ バインド コントロールでデータのフィールドを表すために使用されます。
DataControlField から派生したクラスを使用して、DetailsView や GridView などのデータ バインド コントロールでのデータ フィールドの表示方法を制御します。ASP.NET で提供される各種のデータ コントロール フィールドを次の表に示します。
BoundField | |
ButtonField | データ バインド コントロールにコマンド ボタンを表示します。この種類のデータ コントロール フィールドを使用すると、コントロールに応じて、行または列に Add ボタン、Remove ボタンなどのカスタム ボタン コントロールを表示できます。 |
CheckBoxField | データ バインド コントロールにチェック ボックスを表示します。通常、この種類のデータ コントロール フィールドは、フィールドにブール値を表示する場合に使用されます。 |
CommandField | |
HyperLinkField | データ ソース内のフィールドの値をハイパーリンクとして表示します。この種類のデータ コントロール フィールドを使用すると、2 番目のフィールドをハイパーリンクの URL にバインドできます。 |
ImageField | |
TemplateField |
DataControlField クラスと BoundField クラスを拡張して、独自の種類のデータ コントロール フィールドを作成することもできます。
DataControlField クラスには、ユーザー インターフェイス (UI: User Interface) の要素をデータ バインド コントロールに表示する方法を制御する多数のプロパティが用意されています。UI を表示するときにデータ コントロール フィールドの使用可能なプロパティを使用しないコントロールもあります。たとえば、データ コントロール フィールドを行として表示する DetailsView コントロールには、各データ コントロール フィールドのヘッダー項目は含まれますが、フッター項目は含まれません。そのため、DetailsView コントロールでは FooterText プロパティと FooterStyle プロパティが無視されます。ただし、ShowFooter プロパティを true に設定すると、GridView コントロールは FooterText プロパティと FooterStyle プロパティを使用します。同様に、UI 要素によっては、その表示形式がデータ コントロール フィールドのプロパティの影響を受けます。ItemStyle プロパティは、必ずそのフィールドに適用されます。ButtonField クラスや CheckBoxField クラスのように、DataControlField から派生した型にコントロールが含まれる場合、ControlStyle プロパティがこのフィールドに適用されます。

DataControlField から派生する BoundField オブジェクトと ButtonField オブジェクトを使用して、DetailsView コントロールに行を表示する方法を次のコード例に示します。DetailsView コントロールの AutoGenerateRows プロパティは false に設定されており、SelectCommand プロパティから返されたデータのサブセットを表示できます。
<%@ page language="VB" %> <html> <body> <form runat="server"> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind%>" selectcommand="Select * From Employees"> </asp:sqldatasource> <asp:detailsview id="DetailsView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" height="208px" width="264px" autogeneraterows="False"> <fields> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> <itemstyle backcolor="Yellow"> </itemstyle> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> <itemstyle forecolor="#C00000"> </itemstyle> </asp:boundfield> <asp:buttonfield text="TestButton" buttontype="Button"> </asp:buttonfield> </fields> </asp:detailsview> </form> </body> </html>
<%@ page language="C#" %> <html> <body> <form runat="server"> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind%>" selectcommand="Select * From Employees"> </asp:sqldatasource> <asp:detailsview id="DetailsView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" height="208px" width="264px" autogeneraterows="False"> <fields> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> <itemstyle backcolor="Yellow"> </itemstyle> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> <itemstyle forecolor="#C00000"> </itemstyle> </asp:boundfield> <asp:buttonfield text="TestButton" buttontype="Button"> </asp:buttonfield> </fields> </asp:detailsview> </form> </body> </html>
<%@ page language="VJ#" %> <html> <body> <form runat="server"> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" selectcommand="Select * From Employees"> </asp:sqldatasource> <asp:detailsview id="DetailsView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" height="208px" width="264px" autogeneraterows="False"> <fields> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> <itemstyle backcolor="Yellow"> </itemstyle> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> <itemstyle forecolor="#C00000"> </itemstyle> </asp:boundfield> <asp:buttonfield text="TestButton" buttontype="Button"> </asp:buttonfield> </fields> </asp:detailsview> </form> </body> </html>
BoundField クラスを拡張して、GridView コントロールで使用できるカスタム バインド フィールドを作成する方法を次のコード例に示します。CheckBoxField クラスと同様に、RadioButtonField クラスは true または false のデータ列を表示します。CheckBoxField クラスがバインドされるデータは true 値または false 値の任意のセットでかまいませんが、RadioButtonField クラスがバインドされるデータのセットに含まれる true 値は必ず 1 つだけである必要があります。この例では、DataControlField から派生したすべてのクラスの 2 つの重要なメソッドである ExtractValuesFromCell メソッドと InitializeCell メソッドの実装方法を示しています。
Imports System Imports System.Collections.Specialized 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.VB <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public NotInheritable Class RadioButtonField Inherits CheckBoxField Public Sub New() End Sub 'New ' Gets a default value for a basic design-time experience. Since ' it would look odd, even at design time, to have more than one ' radio button selected, make sure that none are selected. Protected Overrides Function GetDesignTimeValue() As Object Return False End Function ' This method is called by the ExtractRowValues methods of ' GridView and DetailsView. Retrieve the current value of the ' cell from the Checked state of the Radio button. Public Overrides Sub ExtractValuesFromCell( _ ByVal dictionary As IOrderedDictionary, _ ByVal cell As DataControlFieldCell, _ ByVal rowState As DataControlRowState, _ ByVal includeReadOnly As Boolean) ' Determine whether the cell contain a RadioButton ' in its Controls collection. If cell.Controls.Count > 0 Then Dim radio As RadioButton = CType(cell.Controls(0), RadioButton) Dim checkedValue As Object = Nothing If radio Is Nothing Then ' A RadioButton is expected, but a null is encountered. ' Add error handling. Throw New InvalidOperationException( _ "RadioButtonField could not extract control.") Else checkedValue = radio.Checked End If ' Add the value of the Checked attribute of the ' RadioButton to the dictionary. If dictionary.Contains(DataField) Then dictionary(DataField) = checkedValue Else dictionary.Add(DataField, checkedValue) End If End If End Sub ' This method adds a RadioButton control and any other ' content to the cell's Controls collection. Protected Overrides Sub InitializeDataCell( _ ByVal cell As DataControlFieldCell, _ ByVal rowState As DataControlRowState) Dim radio As New RadioButton() ' If the RadioButton is bound to a DataField, add ' the OnDataBindingField method event handler to the ' DataBinding event. If DataField.Length <> 0 Then AddHandler radio.DataBinding, AddressOf Me.OnDataBindField End If radio.Text = Me.Text ' Because the RadioButtonField is a BoundField, it only ' displays data. Therefore, unless the row is in edit mode, ' the RadioButton is displayed as disabled. radio.Enabled = False ' If the row is in edit mode, enable the button. If (rowState And DataControlRowState.Edit) <> 0 _ OrElse (rowState And DataControlRowState.Insert) <> 0 Then radio.Enabled = True End If cell.Controls.Add(radio) End Sub End Class End Namespace
namespace Samples.AspNet.CS { using System; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] public sealed class RadioButtonField : CheckBoxField { public RadioButtonField() { } // Gets a default value for a basic design-time experience. // Since it would look odd, even at design time, to have // more than one radio button selected, make sure that none // are selected. protected override object GetDesignTimeValue() { return false; } // This method is called by the ExtractRowValues methods of // GridView and DetailsView. Retrieve the current value of the // cell from the Checked state of the Radio button. public override void ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) { // Determine whether the cell contains a RadioButton // in its Controls collection. if (cell.Controls.Count > 0) { RadioButton radio = cell.Controls[0] as RadioButton; object checkedValue = null; if (null == radio) { // A RadioButton is expected, but a null is encountered. // Add error handling. throw new InvalidOperationException ("RadioButtonField could not extract control."); } else { checkedValue = radio.Checked; } // Add the value of the Checked attribute of the // RadioButton to the dictionary. if (dictionary.Contains(DataField)) dictionary[DataField] = checkedValue; else dictionary.Add(DataField, checkedValue); } } // This method adds a RadioButton control and any other // content to the cell's Controls collection. protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) { RadioButton radio = new RadioButton(); // If the RadioButton is bound to a DataField, add // the OnDataBindingField method event handler to the // DataBinding event. if (DataField.Length != 0) { radio.DataBinding += new EventHandler(this.OnDataBindField); } radio.Text = this.Text; // Because the RadioButtonField is a BoundField, it only // displays data. Therefore, unless the row is in edit mode, // the RadioButton is displayed as disabled. radio.Enabled = false; // If the row is in edit mode, enable the button. if ((rowState & DataControlRowState.Edit) != 0 || (rowState & DataControlRowState.Insert) != 0) { radio.Enabled = true; } cell.Controls.Add(radio); } } }
package Samples.AspNet; import System.*; import System.Collections.*; import System.Collections.Specialized.*; import System.ComponentModel.*; import System.Web.UI.*; import System.Web.UI.WebControls.*; public class RadioButtonField extends CheckBoxField { public RadioButtonField() { } //RadioButtonField // Gets a default value for a basic design-time experience. //Since it would look odd, // even at design time, to have more than one radio button selected , //make sure that // none are selected. protected Object GetDesignTimeValue() { return (System.Boolean)false; } //GetDesignTimeValue // This method is called by the ExtractRowValues methods on GridView //and DetailsView. Retrieve // the current value of the cell from the Checked state of the Radio button. public void ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, boolean includeReadOnly) throws InvalidOperationException { // Does the cell contain a RadioButton in its Controls collection? if (cell.get_Controls().get_Count() > 0) { RadioButton radio = (RadioButton)cell.get_Controls().get_Item(0); Object checkedValue = null; if (null == radio) { // A RadioButton is expected, // but a null is encountered. Add error handling. throw new InvalidOperationException("RadioButtonField could not" + "extract control."); } else { checkedValue = (System.Boolean)radio.get_Checked(); } // Add the value of the Checked attribute of the // RadioButton to the dictionary. if (dictionary.Contains(get_DataField())) { dictionary.set_Item(get_DataField(), checkedValue); } else { dictionary.Add(get_DataField(), checkedValue); } } } //ExtractValuesFromCell // This method adds a RadioButton control and any other content to the //cell's Controls collection. protected void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) { RadioButton radio = new RadioButton(); // If the RadioButton is bound to a DataField, add // the OnDataBindingField method event handler to the // DataBinding event. if (get_DataField().get_Length() != 0) { radio.add_DataBinding(new EventHandler(this.OnDataBindField)); } radio.set_Text(this.get_Text()); // Because the RadioButtonField is a BoundField, it only displays data. Therefore, // unless the row is in edit mode, the RadioButton is displayed as // disabled. radio.set_Enabled(false); // If the row is in edit mode, enable the button. if (((int)(rowState & DataControlRowState.Edit) != 0) || ((int)( rowState & DataControlRowState.Insert) != 0)) { radio.set_Enabled(true); } cell.get_Controls().Add(radio); } //InitializeDataCell } //RadioButtonField
前の例で使用した RadioButtonField クラスを GridView コントロールで使用する方法を次のコード例に示します。この例では、GridView コントロールにスポーツ チームのデータを表示します。選手のデータは、ID 列、選手名の列、およびチームのキャプテンを true または false で示す列を含むデータ テーブルで管理されます。また、RadioButtonField クラスを使用して、現在のチーム キャプテンであるチーム メンバーを表示します。GridView コントロールを編集して、新しいチーム キャプテンを選択したり、他の選手情報を変更したりできます。
<%@ page language="VB" %> <%@ Register Tagprefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %> <html> <body> <form runat="server"> <asp:gridview id="GridView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" allowsorting="True" autogeneratecolumns="False" autogenerateeditbutton="True" datakeynames="AnID"> <columns> <aspSample:radiobuttonfield headertext="RadioButtonField" text="TeamLeader" datafield="TrueFalse"> </aspSample:radiobuttonfield> <asp:boundfield insertvisible="False" sortexpression="AnID" datafield="AnID" readonly="True" headertext="AnID"> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> </asp:boundfield> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> </asp:boundfield> </columns> </asp:gridview> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind%>" selectcommand="SELECT AnID,FirstName,LastName,TeamLeader FROM Players" updatecommand="UPDATE Players SET TrueFalse='false';UPDATE Players SET TrueFalse='true' WHERE AnID=@anID"> </asp:sqldatasource> </form> </body> </html>
<%@ page language="C#" %> <%@ Register Tagprefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %> <html> <body> <form runat="server"> <asp:gridview id="GridView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" allowsorting="True" autogeneratecolumns="False" autogenerateeditbutton="True" datakeynames="AnID"> <columns> <aspSample:radiobuttonfield headertext="RadioButtonField" text="TeamLeader" datafield="TrueFalse"> </aspSample:radiobuttonfield> <asp:boundfield insertvisible="False" sortexpression="AnID" datafield="AnID" readonly="True" headertext="AnID"> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> </asp:boundfield> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> </asp:boundfield> </columns> </asp:gridview> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind%>" selectcommand="SELECT AnID,FirstName,LastName,TeamLeader FROM Players" updatecommand="UPDATE Players SET TrueFalse='false';UPDATE Players SET TrueFalse='true' WHERE AnID=@anID"> </asp:sqldatasource> </form> </body> </html>
<%@ page language="VJ#" %> <%@ Register Tagprefix="aspSample" Namespace="Samples.AspNet" Assembly="Samples.AspNet.JSL" %> <html> <body> <form runat="server"> <asp:gridview id="GridView1" runat="server" allowpaging="True" datasourceid="SqlDataSource1" allowsorting="True" autogeneratecolumns="False" autogenerateeditbutton="True" datakeynames="AnID"> <columns> <aspSample:radiobuttonfield headertext="RadioButtonField" text="TeamLeader" datafield="TrueFalse"> </aspSample:radiobuttonfield> <asp:boundfield insertvisible="False" sortexpression="AnID" datafield="AnID" readonly="True" headertext="AnID"> </asp:boundfield> <asp:boundfield sortexpression="FirstName" datafield="FirstName" headertext="FirstName"> </asp:boundfield> <asp:boundfield sortexpression="LastName" datafield="LastName" headertext="LastName"> </asp:boundfield> </columns> </asp:gridview> <asp:sqldatasource id="SqlDataSource1" runat="server" Connectionstring="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind" SelectCommand="SELECT AnID,FirstName,LastName,TeamLeader FROM Players" UpdateCommand="UPDATE Players SET TrueFalse='false';UPDATE Players SET TrueFalse='true' WHERE AnID=@anID"> </asp:sqldatasource> </form> </body> </html>

System.Web.UI.WebControls.DataControlField
System.Web.UI.WebControls.BoundField
System.Web.UI.WebControls.ButtonFieldBase
System.Web.UI.WebControls.HyperLinkField
System.Web.UI.WebControls.ImageField
System.Web.UI.WebControls.TemplateField


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataControlField クラスのページへのリンク