共用方式為


HOW TO:在樣板化資料繫結控制項中使用 ASP.NET 動態資料

更新:2007 年 11 月

您可以搭配 ASP.NET 動態資料使用資料繫結控制項,這些控制項會使用樣板 (Template),例如 ListViewFormViewRepeater。使用樣板,您就可以完整控制控制項中資料的配置和外觀,但頁面是與正在使用的資料表相關,因為樣板不能自動產生資料行。當您要建立會使用「動態資料」功能與樣板化資料繫結控制項的自訂頁面時,這個功能就很有用。

使用樣板化資料繫結控制項時,您可以將 DynamicControl 控制項加入您的樣板,即可使用「動態資料」功能。使用 DynamicControl 控制項時,您可以善用下列「動態資料」功能:

除了在樣板化資料繫結控制項中使用 DynamicControl 控制項,您也可以在 GridViewDetailsView 控制項的 TemplateField 欄位中使用這個控制項。

執行此功能的線上範例。

若要加入 DynamicControl 控制項以顯示資料

  1. DynamicControl 控制項加入至資料繫結控制項的其中一個樣板。

    注意事項:

    通常,您會將唯讀欄位加入至樣板化控制項的 ItemTemplate 樣板,因為這個樣板負責顯示資料。不過,若您不希望欄位值被修改,也可以將唯讀欄位加入至其他樣板。

  2. DataField 屬性設定為要顯示之資料行的名稱,如下列範例所示:

    <asp:DynamicControl 
      DataField="ProductName"
       />
    
    注意事項:

    Mode 屬性的預設值為 ReadOnly,所以您不必將此屬性設定為僅顯示資料。

  3. 針對您想要顯示的每個資料欄位,重複執行上述步驟。

若要加入用於編輯作業的 DynamicControl 控制項

  1. DynamicControl 控制項加入至資料繫結控制項或樣板欄位的 EditItemTemplate 樣板。

  2. DataField 屬性設定為要編輯之資料行的名稱。

  3. Mode 屬性設定為 Edit,如下列範例所示:

    <asp:DynamicControl 
      DataField="ProductName"
      Mode="Edit"
       />
    
  4. 針對您想要編輯的每個資料欄位,重複執行上述步驟。

若要加入用於插入作業的 DynamicControl 控制項

  1. DynamicControl 控制項加入至資料繫結控制項或樣板欄位的 InsertItemTemplate 樣板。

  2. DataField 屬性設定為要插入資料之資料行的名稱。

  3. Mode 屬性設定為 Insert,如下列範例所示:

    <asp:DynamicControl 
      DataField="ProductName"
      Mode="Insert"
       />
    
  4. 針對您想要插入資料的每個資料欄位,重複執行上述步驟。

範例

在下列範例中,會示範使用 DynamicControl 控制項來建立自訂頁面 (此自訂頁面會使用「動態資料」功能) 的 FormView 控制項。

<%@ Page Language="VB" %>

<script >

  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    DynamicDataManager1.RegisterControl(FormView1)
  End Sub

</script>

<!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>DynamicControl and Templated Data-Bound Controls Sample</title>
  <link href="~/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <form id="form1" >
    <div>

      <h2>Address Table</h2>

      <asp:DynamicDataManager ID="DynamicDataManager1" 
        AutoLoadForeignKeys="true" />

      <asp:FormView ID="FormView1"  
        DataSourceID="FormDataSource" 
        AllowPaging="true">
        <ItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton"  CommandName="New" CausesValidation="false" Text="New" />
                <asp:LinkButton ID="EditButton"  CommandName="Edit" CausesValidation="false" Text="Edit" />
                <asp:LinkButton ID="DeleteButton"  CommandName="Delete" CausesValidation="false" Text="Delete" />
              </td>
            </tr>
          </table>
        </ItemTemplate>
        <EditItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="UpdateButton"  CommandName="Update">Update</asp:LinkButton>
                <asp:LinkButton ID="CancelEditButton"  CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
              </td>
            </tr>
          </table>
        </EditItemTemplate>
        <InsertItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton"  CommandName="Insert" Text="Insert" />
                <asp:LinkButton ID="CancelInsertButton"  CommandName="Cancel" CausesValidation="false" Text="Cancel" />
              </td>
            </tr>
          </table>
        </InsertItemTemplate>
        <PagerSettings Position="Bottom" Mode="NumericFirstLast" />
      </asp:FormView>

      <!-- This example uses Microsoft SQL Server and connects   -->
      <!-- to the AdventureWorksLT sample database.              -->
      <asp:LinqDataSource ID="FormDataSource" 
        TableName="Addresses" 
        ContextTypeName="AdventureWorksLTDataContext"
        EnableDelete="true"
        EnableInsert="true"
        EnableUpdate="true">
      </asp:LinqDataSource>

    </div>
  </form>
</body>
</html>
<%@ Page Language="C#" %>

<script >
    protected void Page_Init(object sender, EventArgs e) {
        DynamicDataManager1.RegisterControl(FormView1);
    }
</script>

<!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>DynamicControl and Templated Data-Bound Controls Sample</title>
  <link href="~/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <form id="form1" >
    <div>

      <h2>Address Table</h2>

      <asp:DynamicDataManager ID="DynamicDataManager1" 
        AutoLoadForeignKeys="true" />

      <asp:FormView ID="FormView1"  
        DataSourceID="FormDataSource" 
        AllowPaging="true">
        <ItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton"  CommandName="New" CausesValidation="false" Text="New" />
                <asp:LinkButton ID="EditButton"  CommandName="Edit" CausesValidation="false" Text="Edit" />
                <asp:LinkButton ID="DeleteButton"  CommandName="Delete" CausesValidation="false" Text="Delete" />
              </td>
            </tr>
          </table>
        </ItemTemplate>
        <EditItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="UpdateButton"  CommandName="Update">Update</asp:LinkButton>
                <asp:LinkButton ID="CancelEditButton"  CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
              </td>
            </tr>
          </table>
        </EditItemTemplate>
        <InsertItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine1" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl  DataField="AddressLine2" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl  DataField="City" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl  DataField="StateProvince" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl  DataField="CountryRegion" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl  DataField="PostalCode" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton"  CommandName="Insert" Text="Insert" />
                <asp:LinkButton ID="CancelInsertButton"  CommandName="Cancel" CausesValidation="false" Text="Cancel" />
              </td>
            </tr>
          </table>
        </InsertItemTemplate>
        <PagerSettings Position="Bottom" Mode="NumericFirstLast" />
      </asp:FormView>

      <!-- This example uses Microsoft SQL Server and connects   -->
      <!-- to the AdventureWorksLT sample database.              -->
      <asp:LinqDataSource ID="FormDataSource" 
        TableName="Addresses" 
        ContextTypeName="AdventureWorksLTDataContext"
        EnableDelete="true"
        EnableInsert="true"
        EnableUpdate="true">
      </asp:LinqDataSource>

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

編譯程式碼

這項範例需要:

  • 動態資料網站或動態資料 Web 應用程式

  • AdventureWorks 或 AdventureWorksLT 範例資料庫。如需下載並安裝 SQL Server 範例的相關資訊,請參閱 CodePlex 網站上的 Microsoft SQL Server 產品範例:資料庫 (英文)。確定您為所執行的 SQL Server 版本 (SQL Server 2005 或 SQL Server 2008) 安裝正確版本的範例資料庫。

  • LINQ to SQL 類別,設定為存取 AdventureWorks 或 AdventureWorksLT 資料庫的 Address 資料表。

  • 資料內容是使用 Global.asax 檔中的「動態資料」引擎進行註冊。

  • Address 資料表中的 rowguid 和 ModifiedData 資料行設定為在「物件關聯式設計工具」中自動產生。換言之,請確定這兩個資料行的 IsDbGenerated 屬性是設定為 true。只有插入作業才需要此步驟。

穩固程式設計

下列情形可能會造成例外狀況 (Exception):

  • 資料庫無法使用

請參閱

概念

ASP.NET 動態資料概觀

ASP.NET 動態資料模型概觀

參考

DynamicControl

DetailsView

FormView

GridView

ListView

Repeater