Controls You Can Use on Web Forms ASP.NET Data Controls FormView Control
The FormView Web server control is a flexible, full-featured container control used for customizing the display of a single record at a time from a data source. In addition, the FormView can be configured to enable users to add, edit, or delete data in the control.
The FormView control can automatically page over the data, if the underlying data source supports paging.
Like the DataList and the Repeater controls, the FormView does not specify pre-defined layouts for displaying and/or updating the data. Authors must provide the user interfaces by using templates provided for the control.
When the page runs, the FormView renders the content for the record as a FormViewRow object, the appearance of which is described in the template.
The FormView control is typically used in a master/detail scenario where the selected record of the master control determines the record to display or update in the FormView control.
The following sections briefly introduce the main features of the FormView control.
NOTE: The FormView control must be bound to a data source; otherwise, the control will not render on the page.
The FormView control provides the following options for binding to data:
- Data binding using the DataSourceID property, which allows binding the FormView control to a data source control. This is the recommended approach as it enables the FormView control to take advantage of the capabilities of the data source control, and provide built-in functionality for paging and updating.
- Data binding using the DataSource property, which allows binding to any data source that implements the ICollection or IEnumerable interface, including ADO.NET datasets and datareaders. This approach, though, requires coding for any additional functionality such as paging and updating.
NOTE: The FormView control renders only a single row of data at a time, even if its data source exposes multiple records.
When binding to a data source using the DataSourceID property, the FormView control supports two-way data binding. In addition to displaying data, the control automatically supports insert, update, and delete operations on the bound data. For more information, see Binding to Data Using a Data Source Control.
When using the control to allow users to insert, edit, or delete data, you have to set the DataKeyNames property to specify the name(s) of the primary key field(s) that uniquely identify each record in the data source. This information is needed for the automatic update features to work, but does not have to be part of the information displayed in the form.
For details on using the FormView control for updating data, see Allowing Users to Edit Rows in a FormView Control. For general information about data binding in server controls, see Data Binding in Web Forms.
As mentioned, the FormView does not have a built-in user interface. Using templates provided for the control, authors can define how the data associated with the control is to be presented. The following table lists the template types you can specify for the FormView.
Template type |
Description |
EditItemTemplate |
Defines the content for the data row when the FormView control is in edit mode. This template usually contains input controls and command buttons with which the user can edit an existing record. |
EmptyDataTemplate |
Defines the content for the empty data row displayed when the FormView control is bound to a data source that does not contain any records. This template usually contains content to alert the user that the data source does not contain any records. |
FooterTemplate |
Defines the content for the footer row. This template usually contains any additional content you would like to display in the footer row.
NOTE: As an alternative, you can simply specify text to display in the footer row by setting the FooterText property. |
HeaderTemplate |
Defines the content for the header row. This template usually contains any additional content you would like to display in the header row.
NOTE: As an alternative, you can simply specify text to display in the header row by setting the HeaderText property. |
ItemTemplate |
Defines the content for the data row when the FormView control is in read-only mode. This template usually contains content to display the values of an existing record. |
InsertItemTemplate |
Defines the content for the data row when the FormView control is in insert mode. This template usually contains input controls and command buttons with which the user can add a new record. |
PagerTemplate |
Defines the content for the pager row displayed when the paging feature is enabled (when the AllowPaging property is set to true). This template usually contains controls with which the user can navigate to another record.
NOTE: The FormView control has a built-in pager row user interface (UI). You need to create a pager template only if you want to create your own custom pager row. |
At a minimum, you must define an ItemTemplate layout to display the data. For backgorund information, see Adding FormView Controls to a Web Forms Page. For additional information, see Web Server Controls Templates.
To display the value of a field in an item template, use a data binding expression. For more information, see DataBinding Expression Syntax.
To specify the appearance of items in a template, you can set the template’s style. Each template supports its own style object whose properties you can set both at design time and at run time.
The following table lists the style properties you can specify for the FormView.
Style property |
Description |
EditRowStyle |
The style settings for the data row when the FormView control is in edit mode. |
EmptyDataRowStyle |
The style settings for the empty data row displayed in the FormView control when the data source does not contain any records. |
FooterStyle |
The style settings for the footer row of the FormView control. |
HeaderStyle |
The style settings for the header row of the FormView control. |
InsertRowStyle |
The style settings for the data row when the FormView control is in insert mode. |
PagerStyle |
The style settings for the pager row displayed in the FormView control when the paging feature is enabled. |
RowStyle |
The style settings for the data row when the FormView control is in read-only mode. |
For more information, see Web Forms Server Controls and CSS Styles.
Adding FormView Controls to a Web Forms Page