Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

ASP.NET 2.0 Quickstart Tutorials


FormView is a data-bound user interface control that renders a single record at a time from its associated data source, optionally providing paging buttons to navigate between records. It is similar to the DetailsView control, except that it requires the user to define the rendering of each item using templates, instead of using data control fields.

The main difference between DetailsView and FormView is that DetailsView has a built-in tabular rendering, whereas FormView requires a user-defined template for its rendering. The FormView and DetailsView object model are very similar otherwise. The following example demonstrates a FormView control bound to an ObjectDataSource. The ItemTemplate property of FormView contains data-bound Image, Label and HyperLink controls.

VB DataBinding in a FormView Template
Run Sample View Source

Like DetailsView, FormView keeps track of the current item being rendered, and can optionally support paging over multiple data items when the data source returns a list. The following example shows a FormView with paging enabled.

VB DataBinding in a FormView Template (Paged)
Run Sample View Source

The FormView supports automatic Updates, Inserts, and Deletes through its associated data source control just like the DetailsView control. To define the input UI for editing or inserting, you define an EditItemTemplate or InsertItemTemplate in addition to the ItemTemplate. In this template you will data bind input controls such as TextBox, CheckBox, or DropDownList to the fields of the data source. Data bindings in these templates use a "two-way" data binding syntax, however, to allow the FormView to extract the values of input controls from the template in order to pass to the data source. These data bindings use the new Bind(fieldname) syntax instead of Eval.

Important: A control that is data-bound using the Bind syntax must have an ID property set.

The FormView supports the DefaultMode property for specifying the default template to display, but by default the FormView starts in ReadOnly mode and renders the ItemTemplate. To enable UI for transitioning from ReadOnly mode to Edit or Insert mode, you can add a Button control to the template and set its CommandName property to Edit or New. From within the EditItemTemplate, you can add Buttons with CommandName set to Update or Cancel to commit or abort the update operation. Similarly, you can add Buttons with CommandName set to Insert or Cancel to commit or abort the insert operation.

The following example shows a FormView with both an ItemTemplate and EditItemTemplate defined. The ItemTemplate contains controls bound using Eval (one-way), whereas the EditItemTemplate contains a TextBox control two-way bound using a Bind statement. The primary key field (PhotoID) is round-tripped in viewstate using the DataKeyNames property. The FormView contains command buttons for switching between its templates.

VB Two-Way Databinding in a FormView Edit Template
Run Sample View Source

For more information and examples that demonstrate the use of the FormView control, refer to the Performing Data Access section.