DetailsView.Fields 屬性

定義

取得 DataControlField 物件的集合,表示 DetailsView 控制項中明確宣告的資料列欄位。Gets a collection of DataControlField objects that represent the explicitly declared row fields in a DetailsView control.

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 控制項中所有明確宣告的資料列欄位。A DataControlFieldCollection that contains all explicitly declared row fields in the DetailsView control.

屬性

範例

下列程式碼範例示範如何以宣告方式將資料欄欄位加入至 Fields 控制項的集合 DetailsViewThe following code example demonstrates how to declaratively add row fields to the Fields collection of a DetailsView control.


<%@ 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 屬性 (集合) 中。When you explicitly declare the row fields for a DetailsView control, these row fields are stored in the Fields property (collection). Fields集合也可讓您以程式設計方式管理明確宣告的資料列集合。The Fields collection also allows you to programmatically manage the collection of explicitly declared rows.

注意

明確宣告的資料欄欄位可以搭配自動產生的資料欄欄位一起使用。Explicitly declared row fields can be used in combination with automatically generated row fields. 使用這兩者時,會先轉譯明確宣告的資料欄欄位,後面接著自動產生的資料欄欄位。When both are used, explicitly declared row fields are rendered first, followed by the automatically generated row fields. 自動產生的資料欄欄位不會加入至 Fields 集合中。Automatically generated row fields are not added to the Fields collection.

不同的資料欄欄位類型決定控制項中資料列的行為。Different row field types determine the behavior of the rows in the control. 下表顯示可用於集合中的不同資料欄欄位類型 FieldsThe following table shows the different row field types that can be used in the Fields collection.

資料欄欄位類型Row field type 描述Description
BoundField 將資料來源中的欄位值顯示為文字。Displays the value of a field in a data source as text.
ButtonField 在控制項中顯示命令按鈕 DetailsViewDisplays a command button in the DetailsView control. 這可讓您顯示具有自訂按鈕控制項的資料列,例如 [加入] 或 [移除] 按鈕。This allows you to display a row with a custom button control, such as an Add or a Remove button.
CheckBoxField 在控制項中顯示覆選框 DetailsViewDisplays a check box in the DetailsView control. 此資料欄欄位類型通常用來顯示具有布林值的欄位。This row field type is commonly used to display fields with a Boolean value.
CommandField 顯示內建的命令按鈕,以在控制項中執行編輯、插入或刪除作業 DetailsViewDisplays built-in command buttons to perform edit, insert, or delete operations in the DetailsView control.
HyperLinkField 將資料來源中的欄位值顯示為超連結。Displays the value of a field in a data source as a hyperlink. 此資料欄欄位類型可讓您將第二個欄位系結至超連結的 URL。This row field type allows you to bind a second field to the hyperlink's URL.
ImageField 在控制項中顯示影像 DetailsViewDisplays an image in the DetailsView control.
TemplateField 根據指定的範本,顯示控制項中資料列的使用者定義內容 DetailsViewDisplays user-defined content for a row in the DetailsView control according to a specified template. 這個資料欄欄位型別可讓您建立自訂的資料欄欄位。This row field type allows you to create a custom row field.

若要明確宣告控制項的資料欄欄位 DetailsView ,請先將 AutoGenerateRows 屬性設定為 falseTo explicitly declare the row fields for a DetailsView control, first set the AutoGenerateRows property to false. 接下來,在 <Fields> 控制項的開頭和結束記號之間加入開頭和結束記號 DetailsViewNext, add opening and closing <Fields> tags between the opening and closing tags of the DetailsView control. 最後,列出您想要在開頭和結束記號之間包含的資料欄欄位 <Fields>Finally, list the row fields that you want to include between the opening and closing <Fields> tags. 資料欄欄位會 DetailsView 以資料欄欄位出現在集合中的順序,顯示在控制項中 FieldsThe row fields are displayed in the DetailsView control in the order that the row fields appear in the Fields collection.

雖然您可以用程式設計的方式將資料欄欄位加入至 Fields 集合,但是更容易以宣告方式在控制項中列出資料欄欄位, DetailsView 然後使用 Visible 每個資料欄欄位的屬性來顯示或隱藏資料欄欄位。Although you can programmatically add row fields to the Fields collection, it is easier to list the row fields declaratively in the DetailsView control and then use the Visible property of each row field to show or hide the row field.

如果資料 Visible 欄欄位的屬性設定為 false ,資料列就不會顯示在控制項中, DetailsView 資料列的資料也不會對用戶端進行來回行程。If the Visible property of a row field is set to false, the row is not displayed in the DetailsView control and the data for the row does not make a round trip to the client. 如果您想要讓資料列看不到的資料列進行來回行程,請將功能變數名稱加入至 DataKeyNames 屬性。If you want the data for a row that is not visible to make a round trip, add the field name to the DataKeyNames property.

適用於

另請參閱