System.Web.UI.WebControls.FormView Class

Displays the values of a single record from a data source using user-defined templates. The System.Web.UI.WebControls.FormView control allows you to edit, delete, and insert records.

See Also: FormView Members

Syntax

[System.ComponentModel.DefaultEvent("PageIndexChanging")]
[System.Web.UI.ControlValueProperty("SelectedValue")]
[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.FormViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.Web.UI.SupportsEventValidation]
public class FormView : CompositeDataBoundControl, System.Web.UI.IDataItemContainer, System.Web.UI.IPostBackEventHandler, IPostBackContainer

Remarks

In this topic:

Introduction

The System.Web.UI.WebControls.FormView control is used to display a single record from a data source. It is similar to the System.Web.UI.WebControls.DetailsView control, except it displays user-defined templates instead of row fields. Creating your own templates gives you greater flexibility in controlling how the data is displayed. The System.Web.UI.WebControls.FormView control supports the following features:

Templates

For the System.Web.UI.WebControls.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 System.Web.UI.WebControls.FormView control that supports inserting records must have an insert item template defined. The following table lists the different templates that you can create.

FormView.EditItemTemplate

Defines the content for the data row when the System.Web.UI.WebControls.FormView control is in edit mode. This template usually contains input controls and command buttons with which the user can edit an existing record.

FormView.EmptyDataTemplate

Defines the content for the empty data row displayed when the System.Web.UI.WebControls.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.

FormView.FooterTemplate

Defines the content for the footer row. This template usually contains any additional content you would like to display in the footer row.

As an alternative, you can simply specify text to display in the footer row by setting the FormView.FooterText property.
FormView.HeaderTemplate

Defines the content for the header row. This template usually contains any additional content you would like to display in the header row.

As an alternative, you can simply specify text to display in the header row by setting the FormView.HeaderText property.
FormView.ItemTemplate

Defines the content for the data row when the System.Web.UI.WebControls.FormView control is in read-only mode. This template usually contains content to display the values of an existing record.

FormView.InsertItemTemplate

Defines the content for the data row when the System.Web.UI.WebControls.FormView control is in insert mode. This template usually contains input controls and command buttons with which the user can add a new record.

FormView.PagerTemplate

Defines the content for the pager row displayed when the paging feature is enabled (when the FormView.AllowPaging property is set to true). This template usually contains controls with which the user can navigate to another record.

The System.Web.UI.WebControls.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 System.Web.UI.WebControls.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 ASP.NET Data Binding.

Binding to Data

The System.Web.UI.WebControls.FormView control can be bound to a data source control (such as System.Web.UI.WebControls.SqlDataSource, System.Web.UI.WebControls.ObjectDataSource, or System.Web.UI.WebControls.AccessDataSource) or to any data source collection that implements the IEnumerable interface, such as System.Data.DataView, ArrayList, List`1, or other collection types. Use one of the following methods to bind the System.Web.UI.WebControls.FormView control to the appropriate data source type:

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.

Data Operations

The System.Web.UI.WebControls.FormView control provides many built-in capabilities that allow the user to update, delete, insert, and page through items in the control. When the System.Web.UI.WebControls.FormView control is bound to a data source control, the System.Web.UI.WebControls.FormView control can take advantage of the data source control's capabilities and provide automatic updating, deleting, inserting, and paging functionality.

Note:

The System.Web.UI.WebControls.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 System.Web.UI.WebControls.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 System.Web.UI.WebControls.FormView control recognizes certain buttons that have their CommandName properties set to specific values. The following table lists the command buttons that the System.Web.UI.WebControls.FormView control recognizes.

Cancel

"Cancel"

Used in updating or inserting operations to cancel the operation and to discard the values entered by the user. The System.Web.UI.WebControls.FormView control then returns to the mode specified by the FormView.DefaultMode property.

Delete

"Delete"

Used in deleting operations to delete the displayed record from the data source. Raises the FormView.ItemDeleting and FormView.ItemDeleted events.

Edit

"Edit"

Used in updating operations to put the System.Web.UI.WebControls.FormView control in edit mode. The content specified in the FormView.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 FormView.ItemInserting and FormView.ItemInserted events.

New

"New"

Used in inserting operations to put the System.Web.UI.WebControls.FormView control in insert mode. The content specified in the FormView.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 FormView.PageIndexChanging and FormView.PageIndexChanged events.

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 FormView.ItemUpdating and FormView.ItemUpdated events.

Unlike the Delete button (which deletes the displayed record immediately), when the Edit or New button is clicked, the System.Web.UI.WebControls.FormView control goes into edit or insert mode, respectively. In edit mode, the content contained in the FormView.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 System.Web.UI.WebControls.TextBox or a System.Web.UI.WebControls.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 FormView.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 System.Web.UI.WebControls.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 System.Web.UI.WebControls.FormView control that contains the page navigation controls. To enable paging, set the FormView.AllowPaging property to true. You can customize the pager row by setting the properties of objects contained in the FormView.PagerStyle and the FormView.PagerSettings property. Instead of using the built-in pager row UI, you can create your own UI by using the FormView.PagerTemplate property.

Customizing the User Interface

You can customize the appearance of the System.Web.UI.WebControls.FormView control by setting the style properties for the different parts of the control. The following table lists the different style properties.

FormView.EditRowStyle

The style settings for the data row when the System.Web.UI.WebControls.FormView control is in edit mode.

FormView.EmptyDataRowStyle

The style settings for the empty data row displayed in the System.Web.UI.WebControls.FormView control when the data source does not contain any records.

FormView.FooterStyle

The style settings for the footer row of the System.Web.UI.WebControls.FormView control.

FormView.HeaderStyle

The style settings for the header row of the System.Web.UI.WebControls.FormView control.

FormView.InsertRowStyle

The style settings for the data row when the System.Web.UI.WebControls.FormView control is in insert mode.

FormView.PagerStyle

The style settings for the pager row displayed in the System.Web.UI.WebControls.FormView control when the paging feature is enabled.

FormView.RowStyle

The style settings for the data row when the System.Web.UI.WebControls.FormView control is in read-only mode.

Events

The System.Web.UI.WebControls.FormView control provides several events that you can program against. This allows you to run a custom routine whenever an event occurs. The following table lists the events supported by the System.Web.UI.WebControls.FormView control.

FormView.ItemCommand

Occurs when a button within a System.Web.UI.WebControls.FormView control is clicked. This event is often used to perform a task when a button is clicked in the control.

FormView.ItemCreated

Occurs after all System.Web.UI.WebControls.FormViewRow objects are created in the System.Web.UI.WebControls.FormView control. This event is often used to modify the values of a record before it is displayed.

FormView.ItemDeleted

Occurs when a Delete button (a button with its CommandName property set to "Delete") is clicked, but after the System.Web.UI.WebControls.FormView control deletes the record from the data source. This event is often used to check the results of the delete operation.

FormView.ItemDeleting

Occurs when a Delete button is clicked, but before the System.Web.UI.WebControls.FormView control deletes the record from the data source. This event is often used to cancel the delete operation.

FormView.ItemInserted

Occurs when an Insert button (a button with its CommandName property set to "Insert") is clicked, but after the System.Web.UI.WebControls.FormView control inserts the record. This event is often used to check the results of the insert operation.

FormView.ItemInserting

Occurs when an Insert button is clicked, but before the System.Web.UI.WebControls.FormView control inserts the record. This event is often used to cancel the insert operation.

FormView.ItemUpdated

Occurs when an Update button (a button with its CommandName property set to "Update") is clicked, but after the System.Web.UI.WebControls.FormView control updates the row. This event is often used to check the results of the update operation.

FormView.ItemUpdating

Occurs when an Update button is clicked, but before the System.Web.UI.WebControls.FormView control updates the record. This event is often used to cancel the update operation.

FormView.ModeChanged

Occurs after the System.Web.UI.WebControls.FormView control changes modes (to edit, insert, or read-only mode). This event is often used to perform a task when the System.Web.UI.WebControls.FormView control changes modes.

FormView.ModeChanging

Occurs before the System.Web.UI.WebControls.FormView control changes modes (to edit, insert, or read-only mode). This event is often used to cancel a mode change.

FormView.PageIndexChanged

Occurs when one of the pager buttons is clicked, but after the System.Web.UI.WebControls.FormView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different record in the control.

FormView.PageIndexChanging

Occurs when one of the pager buttons is clicked, but before the System.Web.UI.WebControls.FormView control handles the paging operation. This event is often used to cancel the paging operation.

Accessibility

For information about how to configure this control so that it generates markup that conforms to accessibility standards, see Accessibility in Visual Studio 2010 and ASP.NET 4 and ASP.NET Controls and Accessibility.

Applying CSS Styles

The System.Web.UI.WebControls.FormView control lets you specify CSS style rules in markup. If you use templates to customize the appearance of the System.Web.UI.WebControls.FormView control, you can specify CSS styles in the markup in the templates. In that case, no extra outer table is required. You can prevent the table from being rendered by setting the FormView.RenderOuterTable property to false.

Declarative Syntax

Example

<asp:FormView
    AccessKey="string"
    AllowPaging="True|

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0