Learning Resources

Adding a FormView control to the page

Data Binding with the FormView Control

The FormView control provides you with these options for binding to data:

  • Data binding using the DataSourceID property, which allows you to bind the FormView control to a data source control. This is the recommended approach because it allows the FormView control to take advantage of the capabilities of the data source control and to provide built-in functionality for updating and paging.

  • Data binding using the DataSource property, which allows you to bind to various objects, including ADO.NET datasets and data readers. This approach requires you to write the code for any additional functionality such as updating and paging.

When you bind to a data source using the DataSourceID property, the FormView control supports two-way data binding. In addition to the control displaying data, you can enable the control to automatically support insert, update, and delete operations on the bound data.

Creating the FormView Control User Interface

You build the user interface (UI) for the FormView control by creating templates. You specify different templates for different actions. You create an ItemTemplate template for display, insert, and edit modes. You can control paging using a PagerTemplate template, and you can customize the FormView control's header and footer using a HeaderTemplate and FooterTemplate, respectively. Using an EmptyDataTemplate, you can also specify a template to display when the data source does not return any data. 

The item templates that you create for the FormView control specify the content of the control. As with the DetailsView control, you can also customize the display format of the FormView control by using style properties such as the EditRowStyle, EmptyDataRowStyle, FooterStyle, HeaderStyle, InsertRowStyle, PagerStyle, and RowStyle properties.

The following example shows a simple ASP.NET page that uses a FormView control to display data.

<%@ Page language="C#" %>
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/1999/xhtml" >
    FormView Example

"form1" runat="server">

FormView Example

"top"> "ProductsFormView" DataSourceID="ProductsSqlDataSource" AllowPaging="true" runat="server"> "white" backcolor="Blue" />
"right">Product ID: "ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
"right">Product Name: "ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />
"right">Category ID: "CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' />
"right">Quantity Per Unit: "QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' />
"right">Unit Price: "UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' />
"FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/> "PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/> "NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/> "LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/>
"ProductsSqlDataSource" SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice FROM [Products]" connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"/>