FormView Web Server Control Overview
The FormView control gives you the ability to work with a single record from a data source, similar to the DetailsView control. The difference between the FormView and the DetailsView controls is that the DetailsView control uses a tabular layout where each field of the record is displayed as a row of its own. In contrast, the FormView control does not specify a pre-defined layout for displaying the record. Instead, you create a template containing controls to display individual fields from the record. The template contains the formatting, controls, and binding expressions used to create the form.
The FormView control is typically used for updating and inserting new records, and is often used in master/detail scenarios where the selected record of the master control determines the record to display in the FormView control. For more information and an example, see Modifying Data Using a FormView Web Server Control.
The FormView control relies on the capabilities of the data source control to perform tasks such as updating, inserting, and deleting records. The FormView control renders only a single data record at a time, even if its data source exposes multiple records.
The FormView control can automatically page over the data in its associated data source one record at a time, provided that the data is represented by an object implementing the ICollection interface, or that the underlying data source supports paging. The FormView control provides the user interface (UI) for navigating between records. To enable paging behavior, set the AllowPaging property to true and specify a PagerTemplate value.
The FormView control exposes several events that you can handle to execute your own code. The events are raised before and after insert, update, and delete operations of the associated data source control. You can also write handlers for the ItemCreated and ItemCommand events. For more information, see FormView Web Server Control Events.
Note |
---|
The event model of the FormView control is similar to that of the GridView control. However, the FormView control does not support a selection event, because the current record is always the selected item. |
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.
For more information, see Data Source Web Server Controls.
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. For more information, see Creating Templates for the FormView Web Server Control.
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="VB" %>
<!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>FormView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView Example</h3>
<table cellspacing="10">
<tr>
<td valign="top">
<asp:FormView ID="ProductsFormView"
DataSourceID="ProductsSqlDataSource"
AllowPaging="true"
runat="server">
<HeaderStyle forecolor="white" backcolor="Blue" />
<ItemTemplate>
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td><asp:Label id="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><asp:Label id="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td><asp:Label id="CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' /></td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td><asp:Label id="QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' /></td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td><asp:Label id="UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' /></td>
</tr>
</table>
</ItemTemplate>
<PagerTemplate>
<table>
<tr>
<td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>
<td><asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/></td>
<td><asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/></td>
<td><asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/></td>
</tr>
</table>
</PagerTemplate>
</asp:FormView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="ProductsSqlDataSource"
SelectCommand="SELECT * FROM [Products]"
connectionstring="<%$ ConnectionStrings:NorthwindConnection %>"
RunAt="server"/>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!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>FormView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView Example</h3>
<table cellspacing="10">
<tr>
<td valign="top">
<asp:FormView ID="ProductsFormView"
DataSourceID="ProductsSqlDataSource"
AllowPaging="true"
runat="server">
<HeaderStyle forecolor="white" backcolor="Blue" />
<ItemTemplate>
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td><asp:Label id="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><asp:Label id="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td><asp:Label id="CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' /></td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td><asp:Label id="QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' /></td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td><asp:Label id="UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' /></td>
</tr>
</table>
</ItemTemplate>
<PagerTemplate>
<table>
<tr>
<td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>
<td><asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/></td>
<td><asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/></td>
<td><asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/></td>
</tr>
</table>
</PagerTemplate>
</asp:FormView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="ProductsSqlDataSource"
SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice FROM [Products]"
connectionstring="<%$ ConnectionStrings:NorthwindConnection %>"
RunAt="server"/>
</form>
</body>
</html>