次の方法で共有


DetailsView.Fields プロパティ

定義

DetailsView コントロールで明示的に宣言された行フィールドを表すDataControlField オブジェクトのコレクションを取得します。

public:
 virtual property System::Web::UI::WebControls::DataControlFieldCollection ^ Fields { System::Web::UI::WebControls::DataControlFieldCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.DataControlFieldCollection Fields { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.Fields : System.Web.UI.WebControls.DataControlFieldCollection
Public Overridable ReadOnly Property Fields As DataControlFieldCollection

プロパティ値

DetailsView コントロールで明示的に宣言されたすべての行フィールドを含むDataControlFieldCollection

属性

次のコード例では、DetailsView コントロールのFields コレクションに行フィールドを宣言によって追加する方法を示します。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView Fields Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView Fields Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          AutoGenerateRows="false"
          allowpaging="true"
          runat="server">
               
        <Fields>
         <asp:BoundField 
           DataField="CompanyName" 
           HeaderText="Company Name"/>
         <asp:BoundField 
           DataField="City" 
           HeaderText="City"/>
        </Fields>
                    
        </asp:detailsview>
        
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:SqlDataSource ID="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID],
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"

          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] From 
            [Customers]">
        </asp:SqlDataSource>
    </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">
<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView Fields Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView Fields Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          AutoGenerateRows="false"
          allowpaging="true"
          runat="server">
               
        <Fields>
         <asp:BoundField 
           DataField="CompanyName" 
           HeaderText="Company Name"/>
         <asp:BoundField 
           DataField="City" 
           HeaderText="City"/>
        </Fields>
                    
        </asp:detailsview>
        
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:SqlDataSource ID="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID],
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"

          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] From 
            [Customers]">
        </asp:SqlDataSource>
    </form>
  </body>
</html>

注釈

DetailsView コントロールの行フィールドを明示的に宣言すると、これらの行フィールドは Fields プロパティ (コレクション) に格納されます。 Fields コレクションを使用すると、明示的に宣言された行のコレクションをプログラムで管理することもできます。

明示的に宣言された行フィールドは、自動的に生成される行フィールドと組み合わせて使用できます。 両方を使用すると、明示的に宣言された行フィールドが最初にレンダリングされ、その後に自動的に生成された行フィールドが表示されます。 自動生成された行フィールドは、 Fields コレクションには追加されません。

コントロール内の行の動作は、さまざまな行フィールドの種類によって決まります。 次の表に、 Fields コレクションで使用できるさまざまな行フィールドの種類を示します。

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

DetailsView コントロールの行フィールドを明示的に宣言するには、最初に AutoGenerateRows プロパティを false に設定します。 次に、DetailsView コントロールの開始タグと終了タグの間に、開始タグと終了タグ<Fields>タグを追加します。 最後に、開始タグと終了タグの間に含める行フィールド <Fields> 一覧表示します。 行フィールドは、Fields コレクション内の行フィールドが表示される順序で、DetailsView コントロールに表示されます。

Fields コレクションに行フィールドをプログラムで追加することはできますが、DetailsView コントロールで宣言によって行フィールドを一覧表示し、各行フィールドの Visible プロパティを使用して行フィールドを表示または非表示にする方が簡単です。

行フィールドの Visible プロパティが false に設定されている場合、行は DetailsView コントロールに表示されず、行のデータはクライアントへのラウンド トリップを行いません。 ラウンド トリップを行うために表示されない行のデータが必要な場合は、フィールド名を DataKeyNames プロパティに追加します。

適用対象

こちらもご覧ください