DataControlField クラスとは? わかりやすく解説

DataControlField クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

DetailsView、GridView など、表形式データ バインド コントロールデータ列を表すすべての種類データ コントロール フィールド基本クラスとして機能します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public MustInherit Class
 DataControlField
    Implements IStateManager, IDataSourceViewSchemaAccessor
Dim instance As DataControlField
public abstract class DataControlField : IStateManager,
 IDataSourceViewSchemaAccessor
public ref class DataControlField abstract
 : IStateManager, IDataSourceViewSchemaAccessor
public abstract class DataControlField implements
 IStateManager, IDataSourceViewSchemaAccessor
public abstract class DataControlField implements
 IStateManager, IDataSourceViewSchemaAccessor
解説解説

DataControlField クラスは、すべての種類データ コントロール フィールド基本クラスとして機能しますデータ コントロール フィールドは、DataGridColumn オブジェクトが DataGrid コントロールの列の種類を表すのと同様にデータ バインド コントロールデータフィールドを表すために使用されます。

DataControlField から派生したクラス使用してDetailsViewGridView などのデータ バインド コントロールでのデータ フィールド表示方法制御しますASP.NET提供される各種データ コントロール フィールド次の表に示します

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.Object
  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
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataControlField メンバ
System.Web.UI.WebControls 名前空間
BoundField クラス
ButtonFieldBase クラス
CheckBoxField クラス
CommandField クラス
HyperLinkField
ImageField
TemplateField



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataControlField クラス」の関連用語

DataControlField クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataControlField クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS