FormView Web 伺服器控制項概觀

更新:2007 年 11 月

FormView 控制項用於一次顯示資料來源的單一資料錄。使用 FormView 控制項時,您可建立要顯示的範本並編輯資料繫結值。範本要包含控制項、繫結運算式及格式設定,來定義表單的外觀及功能。在主從式案例中,FormView 控制項通常與 GridView 控制項搭配使用。

這個主題包含:

  • 背景

  • 程式碼範例

  • 類別參考

背景

您可以用 FormView 控制項來處理資料來源內的單一記錄,跟 DetailsView 控制項很類似。FormViewDetailsView 控制項之間的差異是 DetailsView 控制項是使用表格式配置,在這種配置中,記錄的每一個欄位都自成一列。相反地,FormView 控制項並不會指定預先定義的配置來顯示記錄。而是建立一個內含控制項的範本,顯示資料錄中的個別欄位。範本會包含建立表單時所使用的格式化、控制項和繫結運算式。

FormView 控制項最常用來更新及插入新的資料錄。此控制項常用於主從式案例中,其中主要控制項裡選取的資料錄會決定 FormView 控制項中要顯示的資料錄。如需詳細資訊和範例,請參閱使用 FormView Web 伺服器控制項修改資料

FormView 控制項依賴資料來源控制項的功能,來執行諸如更新、插入和刪除記錄的工作。即使控制項的資料來源公開多個資料錄,FormView 控制項一次只會顯示單一資料錄。

FormView 控制項會自動把關聯資料來源裡的資料分頁,一次一筆記錄。前提是資料必須由實作 ICollection 介面的物件來表示,或者基礎資料來源可支援分頁。FormView 控制項會提供在記錄之間進行巡覽時所需的使用者介面 (UI)。若要啟用分頁行為,請將 AllowPaging 屬性設定為 true 並指定 PagerTemplate 值。

FormView 控制項公開 (Expose) 多個事件,您可以處理這些事件來執行自己的程式碼。這些事件是在關聯資料來源控制項的插入、更新和刪除作業前後引發。您也可以撰寫 ItemCreatedItemCommand 事件的處理常式。如需詳細資訊,請參閱 FormView Web 伺服器控制項事件

注意事項:

FormView 控制項的事件模型與 GridView 控制項的事件模型很類似。不過,FormView 控制項並不支援選取事件,這是因為目前的記錄一定就是選取的項目。

使用 FormView 控制項的資料繫結

FormView 控制項提供您這些選項來繫結至資料:

  • 您可以用 DataSourceID 屬性來繫結資料,如此,您就可以把 FormView 控制項繫結至資料來源控制項。我們建議您使用此方法,因為這樣一來,FormView 控制項就能運用資料來源控制項的功能,並提供內建的更新和分頁功能。

  • 使用 DataSource 屬性的資料繫結,讓您可以繫結至各種物件,包括 ADO.NET 資料集和資料讀取器 (Reader)。如果使用此方法,您必須撰寫其他功能 (例如更新和分頁) 的程式碼。

當您使用 DataSourceID 屬性繫結至資料來源時,FormView 控制項支援雙向資料繫結。除了顯示資料的控制項外,您也可以啟用控制項自動支援所繫結資料上的插入、更新和刪除作業。

如需詳細資訊,請參閱資料來源 Web 伺服器控制項

建立 FormView 控制項使用者介面

您可以建立範本來建置 (Build) FormView 控制項的使用者介面 (UI)。您可以為不同動作指定不同的範本。您可以建立用於顯示模式、插入模式和編輯模式的 ItemTemplate 範本。您可以使用 PagerTemplate 範本來控制分頁,並分別使用 HeaderTemplateFooterTemplate 來自訂 FormView 控制項的頁首和頁尾 (Footer)。使用 EmptyDataTemplate 時,您還可以指定當資料來源未傳回任何資料時所要顯示範本。如需詳細資訊,請參閱建立 FormView Web 伺服器控制項的範本

您為 FormView 控制項建立的項目樣板會指定控制項的內容。和 DetailsView 控制項一樣,您也可以使用諸如 EditRowStyleEmptyDataRowStyleFooterStyleHeaderStyleInsertRowStylePagerStyleRowStyle 屬性的屬性樣式,來自訂 FormView 的顯示格式。

下列範例是一個 ASP.NET 頁面,該頁面使用 FormView 控制項來顯示資料。

<%@ 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 >
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" >
      <h3>FormView Example</h3>
        <table cellspacing="10"> 
          <tr>               
            <td valign="top">

              <asp:FormView ID="ProductsFormView"
                DataSourceID="ProductsSqlDataSource"
                AllowPaging="true"
                >

                <HeaderStyle forecolor="white" backcolor="Blue" />                

                <ItemTemplate>
                  <table>
                    <tr>
                      <td align="right"><b>Product ID:</b></td>
                      <td><asp:Label id="ProductIDLabel"  Text='<%# Eval("ProductID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Product Name:</b></td>
                      <td><asp:Label id="ProductNameLabel"  Text='<%# Eval("ProductName") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Category ID:</b></td>
                      <td><asp:Label id="CategoryIDLabel"  Text='<%# Eval("CategoryID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Quantity Per Unit:</b></td>
                      <td><asp:Label id="QuantityPerUnitLabel"  Text='<%# Eval("QuantityPerUnit") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Unit Price:</b></td>
                      <td><asp:Label id="UnitPriceLabel"  Text='<%# Eval("UnitPrice") %>' /></td>
                    </tr>
                  </table>                 
                </ItemTemplate>

                <PagerTemplate>
                  <table>
                    <tr>
                      <td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>
                      <td><asp:LinkButton ID="PrevButton"  CommandName="Page" CommandArgument="Prev"  Text="<"  RunAt="server"/></td>
                      <td><asp:LinkButton ID="NextButton"  CommandName="Page" CommandArgument="Next"  Text=">"  RunAt="server"/></td>
                      <td><asp:LinkButton ID="LastButton"  CommandName="Page" CommandArgument="Last"  Text=">>" RunAt="server"/></td>
                    </tr>
                  </table>
                </PagerTemplate>

              </asp:FormView>

            </td>
          </tr>
        </table>

        <asp:SqlDataSource ID="ProductsSqlDataSource" 
          SelectCommand="SELECT * FROM [Products]" 
          connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" 
          RunAt="server"/>

      </form>
  </body>
</html>
<%@ 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 >
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" >
      <h3>FormView Example</h3>
        <table cellspacing="10"> 
          <tr>               
            <td valign="top">

              <asp:FormView ID="ProductsFormView"
                DataSourceID="ProductsSqlDataSource"
                AllowPaging="true"
                >

                <HeaderStyle forecolor="white" backcolor="Blue" />                

                <ItemTemplate>
                  <table>
                    <tr>
                      <td align="right"><b>Product ID:</b></td>
                      <td><asp:Label id="ProductIDLabel"  Text='<%# Eval("ProductID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Product Name:</b></td>
                      <td><asp:Label id="ProductNameLabel"  Text='<%# Eval("ProductName") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Category ID:</b></td>
                      <td><asp:Label id="CategoryIDLabel"  Text='<%# Eval("CategoryID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Quantity Per Unit:</b></td>
                      <td><asp:Label id="QuantityPerUnitLabel"  Text='<%# Eval("QuantityPerUnit") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Unit Price:</b></td>
                      <td><asp:Label id="UnitPriceLabel"  Text='<%# Eval("UnitPrice") %>' /></td>
                    </tr>
                  </table>                 
                </ItemTemplate>

                <PagerTemplate>
                  <table>
                    <tr>
                      <td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>
                      <td><asp:LinkButton ID="PrevButton"  CommandName="Page" CommandArgument="Prev"  Text="<"  RunAt="server"/></td>
                      <td><asp:LinkButton ID="NextButton"  CommandName="Page" CommandArgument="Next"  Text=">"  RunAt="server"/></td>
                      <td><asp:LinkButton ID="LastButton"  CommandName="Page" CommandArgument="Last"  Text=">>" RunAt="server"/></td>
                    </tr>
                  </table>
                </PagerTemplate>

              </asp:FormView>

            </td>
          </tr>
        </table>

        <asp:SqlDataSource ID="ProductsSqlDataSource" 
          SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice FROM [Products]" 
          connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" 
          RunAt="server"/>

      </form>
  </body>
</html>

回到頁首

程式碼範例

建立 FormView Web 伺服器控制項的範本

在 FormView Web 伺服器控制項置入分頁

使用 FormView Web 伺服器控制項修改資料

FormView Web 伺服器控制項事件

逐步解說:使用 FormView Web 伺服器控制項顯示 Web 網頁中已格式化的資料

回到頁首

類別參考

下表列出與 FormView 控制項相關的重要類別。

成員

描述

FormView

控制項的主要類別。

回到頁首

請參閱

概念

ASP.NET 資料存取概觀

ASP.NET Web 伺服器控制項樣板

其他資源

資料工具箱控制項