DetailsView.Fields 屬性

定義

取得 DataControlField 物件的集合,表示 DetailsView 控制項中明確宣告的資料列欄位。

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

屬性值

DataControlFieldCollection

DataControlFieldCollection,包含 DetailsView 控制項中所有明確宣告的資料列欄位。

屬性

範例

下列程式碼範例示範如何以宣告方式將資料欄欄位 Fields 加入控制項的 DetailsView 集合。


<%@ 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 將資料來源中的欄位值顯示為超連結。 此資料欄欄位類型可讓您將第二個欄位系結至超連結的 URL。
ImageField 在 控制項中 DetailsView 顯示影像。
TemplateField 根據指定的範本,顯示控制項中資料列的使用者 DetailsView 定義內容。 此資料欄欄位類型可讓您建立自訂資料欄欄位。

若要明確宣告控制項的資料 DetailsView 欄欄位,請先將 AutoGenerateRows 屬性設定為 false 。 接下來,在控制項的 DetailsView 開頭和結束記號之間新增開頭和結尾 <Fields> 標記。 最後,列出您要在開頭和結尾 <Fields> 標記之間包含的資料欄欄位。 資料欄欄位會 DetailsView 以資料欄欄位出現在集合中的順序顯示在 控制項中 Fields

雖然您可以以程式設計方式將資料欄欄位 Fields 加入至集合,但是以宣告方式在 控制項中 DetailsView 列出資料欄欄位,然後使用 Visible 每個資料欄欄位的 屬性來顯示或隱藏資料欄欄位會比較容易。

如果資料 Visible 欄欄位的 屬性設定 false 為 ,則資料列不會顯示在 控制項中 DetailsView ,而且資料列的資料不會往返用戶端。 如果您想要讓資料列的資料不可見來回行程,請將功能變數名稱新增至 DataKeyNames 屬性。

適用於

另請參閱