次の方法で共有


How to: Use ASP.NET Dynamic Data in Templated Data-Bound Controls

You can use ASP.NET Dynamic Data with data-bound controls that use templates such as ListView, FormView and Repeater. By working with templates, you can have complete control over the layout and appearance of the data in the control, but the pages are specific for the table you are working with since the templates cannot automatically generate columns. This is useful when you want to create custom pages that use Dynamic Data features with the templated data-bound controls.

When you work with templated data-bound controls, you can add the DynamicControl control to your templates to make use of the Dynamic Data functionality. When you use a DynamicControl control, you take advantage of the following Dynamic Data features:

In addition to using the DynamicControl control in templated data-bound controls, you can use it in a TemplateField field of a GridView or a DetailsView control.

See a run-time code example of this feature: Run.

To add a DynamicControl control to display data

  1. Add a DynamicControl control to one of the templates of the data-bound control.

    Note

    Usually you add read-only fields in the ItemTemplate template of templated controls, because that is the template responsible for displaying data. However, you can also add a read-only field to other templates when you do not want the field value to be modified.

  2. Set the DataField property to the name of the column that you want to display, as shown in the following example:

    <asp:DynamicControl 
      DataField="ProductName"
      runat="server" />
    

    Note

    The default value for the Mode property is ReadOnly, so you do not have to set this property to display data only.

  3. Repeat the preceding steps for each data field that you want to display.

To add a DynamicControl control for edit operations

  1. Add a DynamicControl control to the EditItemTemplate template of the data-bound control or template field.

  2. Set the DataField property to the name of the column that you want to edit.

  3. Set the Mode property to the Edit, as shown in the following example:

    <asp:DynamicControl 
      DataField="ProductName"
      Mode="Edit"
      runat="server" />
    
  4. Repeat the preceding steps for each data field that you want to edit.

To add a DynamicControl control for insert operations

  1. Add a DynamicControl control to the InsertItemTemplate template of the data-bound control or template field.

  2. Set the DataField property to the name of the column that you want to insert data for.

  3. Set the Mode property to the Insert, as shown in the following example:

    <asp:DynamicControl 
      DataField="ProductName"
      Mode="Insert"
      runat="server" />
    
  4. Repeat the preceding steps for each data field that you want to insert data for.

Example

The following example shows a FormView control that uses DynamicControl controls to create a custom page that uses Dynamic Data features.

<%@ Page Language="VB" %>

<script runat="server">

  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 runat="server">
  <title>DynamicControl and Templated Data-Bound Controls Sample</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>

      <h2>Address Table</h2>

      <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true" 
        HeaderText="List of validation errors"  />
      <asp:DynamicValidator runat="server" ID="DynamicValidator1"
        ControlToValidate="FormView1" Display="None" />

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

      <asp:FormView ID="FormView1" runat="server" 
        DataSourceID="FormDataSource" 
        AllowPaging="true">
        <ItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton" runat="server" CommandName="New" CausesValidation="false" Text="New" />
                <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" CausesValidation="false" Text="Edit" />
                <asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" CausesValidation="false" Text="Delete" />
              </td>
            </tr>
          </table>

        </ItemTemplate>
        <EditItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update">Update</asp:LinkButton>
                <asp:LinkButton ID="CancelEditButton" runat="server" CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
              </td>
            </tr>
          </table>

        </EditItemTemplate>
        <InsertItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                <asp:LinkButton ID="CancelInsertButton" runat="server" 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" runat="server"
        TableName="Addresses" 
        ContextTypeName="AdventureWorksLTDataContext"
        EnableDelete="true"
        EnableInsert="true"
        EnableUpdate="true">
      </asp:LinqDataSource>

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

<script runat="server">
    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 runat="server">
  <title>DynamicControl and Templated Data-Bound Controls Sample</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>

      <h2>Address Table</h2>

      <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true" 
        HeaderText="List of validation errors"  />
      <asp:DynamicValidator runat="server" ID="DynamicValidator1"
        ControlToValidate="FormView1" Display="None" />

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

      <asp:FormView ID="FormView1" runat="server" 
        DataSourceID="FormDataSource" 
        AllowPaging="true">
        <ItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton" runat="server" CommandName="New" CausesValidation="false" Text="New" />
                <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" CausesValidation="false" Text="Edit" />
                <asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" CausesValidation="false" Text="Delete" />
              </td>
            </tr>
          </table>

        </ItemTemplate>
        <EditItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" Mode="Edit" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update">Update</asp:LinkButton>
                <asp:LinkButton ID="CancelEditButton" runat="server" CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
              </td>
            </tr>
          </table>

        </EditItemTemplate>
        <InsertItemTemplate>
          <table>
            <tr>
              <td>Address Line 1:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine1" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Address Line 2:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="AddressLine2" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>City:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="City" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>State/Province:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="StateProvince" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="CountryRegion" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td>Postal Code:</td>
              <td>
                <asp:DynamicControl runat="server" DataField="PostalCode" Mode="Insert" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                <asp:LinkButton ID="CancelInsertButton" runat="server" 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" runat="server"
        TableName="Addresses" 
        ContextTypeName="AdventureWorksLTDataContext"
        EnableDelete="true"
        EnableInsert="true"
        EnableUpdate="true">
      </asp:LinqDataSource>

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

Compiling the Code

This example requires:

  • A Dynamic Data Web site or a Dynamic Data Web application.

  • Either the AdventureWorks or the AdventureWorksLT sample database. For information about how to download and install the SQL Server sample, see Microsoft SQL Server Product Samples: Database on the CodePlex site. Make sure that you install the correct version of the sample database for the version of SQL Server that you are running (SQL Server 2005 or SQL Server 2008).

  • A LINQ to SQL class that is configured to access the Address table of the AdventureWorks or AdventureWorksLT database.

  • The data context is registered with the Dynamic Data engine in the Global.asax file.

  • The columns rowguid and ModifiedData from the Address table are set to be auto generated in the Object Relational designer. In other words, make sure that the IsDbGenerated property for these two columns is set to true. This is required for the insert operation only.

Robust Programming

The following conditions may cause an exception:

  • The database is unavailable.

See Also

Concepts

ASP.NET Dynamic Data Overview

ASP.NET Dynamic Data Model Overview

Reference

DynamicControl

DetailsView

FormView

GridView

ListView

Repeater

Change History

Date

History

Reason

July 2008

Added topic for new feature.

SP1 feature change.