Partager via


How to: Bind to Data in a Templated Control

The FormView, DataList, Repeater, and ListView Web server controls use templates to display data and to retrieve user input to insert, update, or delete data. In addition, you can use templates with the GridView and DetailsView controls to customize data layout.

You can bind a templated control to a data source control such as the LinqDataSource, ObjectDataSource or SqlDataSource control by setting the templated control's DataSourceID property to the ID of the data source control. You can then use the Eval and Bind functions within the template to bind to data from the data source. For more information, see Data-Binding Expression Syntax.

To bind a control to data using templates

  1. Add a data source control such as the SqlDataSource control to the page, as in the following example:

    <asp:SqlDataSource ID="SqlDataSource1" 
      SelectCommand="SELECT * FROM [Products]"
      ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
      RunAt="server">
    </asp:SqlDataSource>
    
    <asp:SqlDataSource ID="SqlDataSource1" 
      SelectCommand="SELECT * FROM [Products]"
      ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
      RunAt="server">
    </asp:SqlDataSource>
    
  2. Add a control that supports templates, such as the ASP.NET FormView control.

  3. Set the templated control's DataSourceID property to the ID of the data source control from Step 1, as in this example:

    <asp:FormView ID="FormView1"
      DataSourceID="SqlDataSource1"
      DataKeyNames="ProductID"     
      Runat="server">
    </asp:FormView>
    
  4. Add templates to the templated control and populate them with controls and markup.

  5. To display data, use the Eval function as a property setting and reference the bound data field. In templates used to insert or edit data, use the Bind function to reference the data bound field, as shown in the following example:

    <asp:FormView ID="FormView1"
      DataSourceID="SqlDataSource1"
      DataKeyNames="ProductID"     
      RunAt="server">
    
      <ItemTemplate>
        <table>
          <tr>
            <td align="right"><b>Product ID:</b></td>       
            <td><%# Eval("ProductID") %></td>
          </tr>
          <tr>
            <td align="right"><b>Product Name:</b></td>     
            <td><%# Eval("ProductName") %></td>
          </tr>
          <tr>
            <td align="right"><b>Category ID:</b></td>      
            <td><%# Eval("CategoryID") %></td>
          </tr>
          <tr>
            <td align="right"><b>Quantity Per Unit:</b></td>
            <td><%# Eval("QuantityPerUnit") %></td>
          </tr>
          <tr>
            <td align="right"><b>Unit Price:</b></td>       
            <td><%# Eval("UnitPrice") %></td>
          </tr>
        </table>                 
    
      </ItemTemplate>                   
    </asp:FormView>
    
    <asp:FormView ID="FormView1"
      DataSourceID="SqlDataSource1"
      DataKeyNames="ProductID"     
      RunAt="server">
    
      <ItemTemplate>
        <table>
          <tr>
            <td align="right"><b>Product ID:</b></td>       
            <td><%# Eval("ProductID") %></td>
          </tr>
          <tr>
            <td align="right"><b>Product Name:</b></td>     
            <td><%# Eval("ProductName") %></td>
          </tr>
          <tr>
            <td align="right"><b>Category ID:</b></td>      
            <td><%# Eval("CategoryID") %></td>
          </tr>
          <tr>
            <td align="right"><b>Quantity Per Unit:</b></td>
            <td><%# Eval("QuantityPerUnit") %></td>
          </tr>
          <tr>
            <td align="right"><b>Unit Price:</b></td>       
            <td><%# Eval("UnitPrice") %></td>
          </tr>
        </table>                 
    
      </ItemTemplate>                 
    </asp:FormView>
    

    Each Web server control supports different templates. For example, the Repeater control supports an ItemTemplate and an AlternatingItemTemplate to display data using alternating controls, styles, and markup. For details on Web server controls and supported templates, see ASP.NET Data-Bound Web Server Controls Overview.

See Also

Tasks

How to: Connect to an ODBC Database Using the SqlDataSource Control

Concepts

Data Source Controls Overview