Export (0) Print
Expand All

How to: Bind to Data in a Templated Control

The FormView, DataList, and Repeater 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 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:

    No code example is currently available or this language may not be supported.
  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:

    No code example is currently available or this language may not be supported.

    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

Community Additions

ADD
Show:
© 2014 Microsoft