For the FormView control to display content, you need to create templates for the different parts of the control. Most templates are optional; however, you must create a template for the mode in which the control is configured. For example, a FormView control that supports inserting records must have an insert item template defined. The following table lists the different templates that you can create.
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. |
To display the value of a field in an item template, use a data binding expression. For more information on data binding expressions, see Data-Binding Expression Syntax.
Input controls in the edit item template and insert item template can be bound to the fields of a data source by using a two-way binding expression. This allows the FormView control to automatically extract the values of the input control for an update or insert operation. Two-way binding expressions also allow input controls in an edit item template to automatically display the original field values. For more information on two-way binding expressions, see Binding to Databases.
The FormView control can be bound to a data source control (such as SqlDataSource, AccessDataSource, ObjectDataSource and so on), or to any data source that implements the System.Collections..::.IEnumerable interface (such as System.Data..::.DataView, System.Collections..::.ArrayList, and System.Collections..::.Hashtable). Use one of the following methods to bind the FormView control to the appropriate data source type:
To bind to a data source control, set the DataSourceID property of the FormView control to the ID value of the data source control. The FormView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform inserting, updating, deleting, and paging functionality. This is the preferred method to bind to data.
To bind to a data source that implements the System.Collections..::.IEnumerable interface, programmatically set the DataSource property of the FormView control to the data source and then call the DataBind method. When using this method, the FormView control does not provide built-in inserting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.
For more information on data binding, see Accessing Data with ASP.NET.
Note: |
|---|
This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. Whenever possible, it is strongly recommended that values are HTML-encoded before they are displayed in this control. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Introduction to the Validation Controls. |
The FormView control provides many built-in capabilities that allow the user to update, delete, insert, and page through items in the control. When the FormView control is bound to a data source control, the FormView control can take advantage of the data source control's capabilities and provide automatic updating, deleting, inserting, and paging functionality.
Note: |
|---|
The FormView control can provide support for update, delete, insert, and paging operations with other types of data sources; however, you must provide an appropriate event handler with the implementation for these operations. |
Because the FormView control uses templates, it does not provide a way to automatically generate command buttons to perform updating, deleting, or inserting operations. You must manually include these command buttons in the appropriate template. The FormView control recognizes certain buttons that have their CommandName properties set to specific values. The following table lists the command buttons that the FormView control recognizes.
Button | CommandName value | Description |
|---|
Cancel | "Cancel" | Used in updating or inserting operations to cancel the operation and to discard the values entered by the user. The FormView control then returns to the mode specified by the DefaultMode property. |
Delete | "Delete" | Used in deleting operations to delete the displayed record from the data source. Raises the ItemDeleting and ItemDeleted events. |
Edit | "Edit" | Used in updating operations to put the FormView control in edit mode. The content specified in the EditItemTemplate property is displayed for the data row. |
Insert | "Insert" | Used in inserting operations to attempt to insert a new record in the data source using the values provided by the user. Raises the ItemInserting and ItemInserted events. |
New | "New" | Used in inserting operations to put the FormView control in insert mode. The content specified in the InsertItemTemplate property is displayed for the data row. |
Page | "Page" | Used in paging operations to represent a button in the pager row that performs paging. To specify the paging operation, set the CommandArgument property of the button to "Next", "Prev", "First", "Last", or the index of the page to which to navigate. Raises the PageIndexChanging and PageIndexChanged events. Note:This type of button is typically used only in the pager template. |
Update | "Update" | Used in updating operations to attempt to update the displayed record in the data source with the values provided by the user. Raises the ItemUpdating and ItemUpdated events. |
Unlike the Delete button (which deletes the displayed record immediately), when the Edit or New button is clicked, the FormView control goes into edit or insert mode, respectively. In edit mode, the content contained in the EditItemTemplate property is displayed for the current data item. Typically, the edit item template is defined such that the Edit button is replaced with an Update and a Cancel button. Input controls that are appropriate for the field's data type (such as a TextBox or a CheckBox control) are also usually displayed with a field's value for the user to modify. Clicking the Update button updates the record in the data source, while clicking the Cancel button abandons any changes.
Likewise, the content contained in the InsertItemTemplate property is displayed for the data item when the control is in insert mode. The insert item template is typically defined such that the New button is replaced with an Insert and a Cancel button, and empty input controls are displayed for the user to enter the values for the new record. Clicking the Insert button inserts the record in the data source, while clicking the Cancel button abandons any changes.
The FormView control provides a paging feature, which allows the user to navigate to other records in the data source. When enabled, a pager row is displayed in the FormView control that contains the page navigation controls. To enable paging, set the AllowPaging property to true. You can customize the pager row by setting the properties of objects contained in the PagerStyle and the PagerSettings property. Instead of using the built-in pager row UI, you can create your own UI by using the PagerTemplate property.