DataControlField クラス

定義

すべてのデータ コントロールフィールド型の基底クラスとして機能します。これは、DetailsViewGridView など、表形式のデータ バインド コントロール内のデータ列を表します。

public ref class DataControlField abstract : System::Web::UI::IDataSourceViewSchemaAccessor, System::Web::UI::IStateManager
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public abstract class DataControlField : System.Web.UI.IDataSourceViewSchemaAccessor, System.Web.UI.IStateManager
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))>]
type DataControlField = class
    interface IStateManager
    interface IDataSourceViewSchemaAccessor
Public MustInherit Class DataControlField
Implements IDataSourceViewSchemaAccessor, IStateManager
継承
DataControlField
派生
属性
実装

次のコード例は、コントロール内の行を表示するために使用 BoundField する方法と ButtonField 、そこから DataControlField派生したオブジェクトを DetailsView 示しています。 DetailsViewコントロールにはプロパティがAutoGenerateRows設定falseされています。これにより、プロパティによって返されるデータのサブセットをSelectCommand表示できます。

<%@ page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="form1" 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="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="form1" 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>

次のコード例では、クラスを拡張 BoundField して、コントロールで使用できるカスタム バインド フィールドを作成する方法を GridView 示します。 クラスと同様に CheckBoxField 、クラスは RadioButtonFieldtrue または false データを表します。 ただし、クラスがバインドされる CheckBoxField データには任意の true セットまたは false 値を指定できますが、クラスにバインドされる RadioButtonField データのセットは、いつでも 1 つの true 値しか持つことができません。 この例では、すべてのクラスから派生DataControlFieldした ExtractValuesFromCell 2 つの重要なメソッドとInitializeCellメソッドを実装する方法を示します。

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);
    }
  }
}
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

        ' 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

次のコード例は、前の例で示したクラスを RadioButtonField コントロールで使用する方法を GridView 示しています。 この例では、コントロールに GridView スポーツ チームのデータが表示されます。 プレイヤー データは、ID 列、プレイヤー名の列、チームのキャプテンを識別する true 列または false 列を含むデータ テーブルに保持されます。 この RadioButtonField クラスは、現在のチーム キャプテンであるチーム メンバーを表示するために使用されます。 コントロールを GridView 編集して、新しいチーム キャプテンを選択したり、他のプレイヤー情報を変更したりすることができます。

<%@ page language="C#" %>
<%@ Register Tagprefix="aspSample"
             Namespace="Samples.AspNet.CS"
             Assembly="Samples.AspNet.CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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="VB" %>
<%@ Register Tagprefix="aspSample"
             Namespace="Samples.AspNet.VB"
             Assembly="Samples.AspNet.VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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>

注釈

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

派生した DataControlField クラスを使用して、データのフィールドをデータ バインド コントロール (または . など) に表示する方法を DetailsView 制御します GridView。 次の表に、ASP.NET によって提供されるさまざまなデータ制御フィールドの種類を示します。

列フィールドの種類 [説明]
BoundField データ ソース内のフィールドの値をテキストとして表示します。
ButtonField データ バインド コントロールにコマンド ボタンを表示します。 コントロールに応じて、行または列をカスタム ボタン コントロール ([追加] や [削除] ボタンなど) で表示できます。
CheckBoxField データ バインド コントロールにチェック ボックスを表示します。 このデータコントロールフィールドタイプは、ブール値を持つフィールドを表示するために一般的に使用されます。
CommandField データ バインド コントロールで編集、挿入、または削除操作を実行するための組み込みのコマンド ボタンを表示します。
HyperLinkField データ ソース内のフィールドの値をハイパーリンクとして表示します。 このデータ 制御フィールド型を使用すると、2 番目のフィールドをハイパーリンクの URL にバインドできます。
ImageField データ バインド コントロールに画像を表示します。
TemplateField 指定したテンプレートに従って、データ バインド コントロールにユーザー定義コンテンツを表示します。

また、クラスとBoundFieldクラスをDataControlField拡張して、独自のデータ制御フィールド型を作成することもできます。

このクラスには DataControlField 、データ バインド コントロールにユーザー インターフェイス (UI) 要素を表示する方法を決定する多くのプロパティが用意されています。 UI をレンダリングするときに、すべてのコントロールで使用可能なすべてのデータ コントロール フィールド プロパティが使用されるわけではありません。 たとえば、データ コントロール フィールドを DetailsView 行として表示するコントロールには、各データ コントロール フィールドのヘッダー項目が含まれますが、フッター項目は含まれます。 したがって、 FooterText コントロールと FooterStyle プロパティは無視されます DetailsView 。 ただし、このコントロールではGridView、プロパティが にFooterText設定trueされている場合は、プロパティとFooterStyleプロパティがShowFooter使用されます。 同様に、データ コントロール フィールドのプロパティは、要素の内容に応じて UI 要素の表示に影響します。 この ItemStyle プロパティは常にフィールドに適用されます。 派生したDataControlField型にコントロールが含まれている場合(クラスCheckBoxFieldなどButtonField)、ControlStyleプロパティがフィールドに適用されます。

コンストラクター

DataControlField()

DataControlField クラスの新しいインスタンスを初期化します。

プロパティ

AccessibleHeaderText

一部のコントロールの AbbreviatedText プロパティ値として表示されるテキストを取得または設定します。

Control

DataControlField オブジェクトが関連付けられているデータ コントロールの参照を取得します。

ControlStyle

DataControlField オブジェクトに格納されているすべての Web サーバー コントロールのスタイルを取得します。

DesignMode

デザイン時環境で、現在データ コントロール フィールドが表示されているかどうかを示す値を取得します。

FooterStyle

データ コントロール フィールドのフッターのスタイルを取得または設定します。

FooterText

データ コントロール フィールドのフッター項目に表示されるテキストを取得または設定します。

HeaderImageUrl

データ コントロール フィールドのヘッダー項目に表示されるイメージの URL を取得または設定します。

HeaderStyle

データ コントロール フィールドのヘッダーのスタイルを取得または設定します。

HeaderText

データ コントロール フィールドのヘッダー項目に表示されるテキストを取得または設定します。

InsertVisible

DataControlField オブジェクトの親データ バインド コントロールが挿入モードの場合に、このオブジェクトが表示されるかどうかを示す値を取得します。

IsTrackingViewState

DataControlField オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

ItemStyle

データ コントロール フィールドで表示されるテキスト ベースの内容のスタイルを取得します。

ShowHeader

データ コントロール フィールドのヘッダー項目を表示するかどうかを示す値を取得または設定します。

SortExpression

データ ソース コントロールでデータを並べ替えるために使用される並べ替え式を、取得または設定します。

ValidateRequestMode

コントロールでクライアントの入力を検証するかどうかを指定する値を取得または設定します。

ViewState

同一のページに対する複数の要求にわたって、DataControlField オブジェクトのビューステートを保存し、復元できるようにする状態情報のディクショナリを取得します。

Visible

データ コントロール フィールドを表示するかどうかを示す値を取得または設定します。

メソッド

CloneField()

現在の DataControlField 派生オブジェクトのコピーを作成します。

CopyProperties(DataControlField)

現在の DataControlField 派生オブジェクトのプロパティを、指定された DataControlField オブジェクトにコピーします。

CreateField()

派生クラス内でオーバーライドされた場合は、空の DataControlField 派生オブジェクトを作成します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
ExtractValuesFromCell(IOrderedDictionary, DataControlFieldCell, DataControlRowState, Boolean)

現在のテーブル セルからデータ コントロール フィールドの値を抽出し、指定した IDictionary コレクションにその値を追加します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
Initialize(Boolean, Control)

データ コントロール フィールドのインスタンスの基本的な初期化を実行します。

InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

セルのコントロールのコレクションにテキストまたはコントロールを追加します。

LoadViewState(Object)

データ ソース ビューの、以前保存したビューステートを復元します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnFieldChanged()

FieldChanged イベントを発生させます。

SaveViewState()

ページがサーバーにポストバックされた時間以降に発生した、DataControlField ビューステートへの変更を保存します。

ToString()

この DataControlField オブジェクトを表す文字列を返します。

TrackViewState()

DataControlField オブジェクトがそのビューステートの変更を追跡するようにします。それにより、変更をコントロールの ViewState プロパティに格納して、同じページに対する複数の要求にわたって永続化できます。

ValidateSupportsCallback()

派生クラス内でオーバーライドされた場合は、フィールドに格納されているコントロールがコールバックをサポートすることを通知します。

明示的なインターフェイスの実装

IDataSourceViewSchemaAccessor.DataSourceViewSchema

DataControlField オブジェクトに関連付けられているスキーマを取得または設定します。

IStateManager.IsTrackingViewState

DataControlField オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

IStateManager.LoadViewState(Object)

データ コントロール フィールドの、以前保存したビューステートを復元します。

IStateManager.SaveViewState()

ページがサーバーにポストバックされた時間以降に発生した、DataControlField ビューステートへの変更を保存します。

IStateManager.TrackViewState()

DataControlField オブジェクトがそのビューステートの変更を追跡するようにします。それにより、変更をコントロールの ViewState プロパティに格納して、同じページに対する複数の要求にわたって永続化できます。

適用対象

こちらもご覧ください