The ASP.NET 2.0 FormView control is similar to the DetailsView control in that it conveniently displays individual records from a back-end data source. This article will discuss the syntax and application of this control.
Customization
Although both controls display one record at a time, the key difference between DetailsView and FormView is that FormView utilizes user-defined templates; DetailsView uses row fields. The FormView control does not predefine the data layout; instead, you build a template containing the control to display individual fields from the record. The template contains the formats, controls and binding expressions used to create the form.
You can control the display of data records in three forms: edit, view, and add a new record. Additionally, you can include and format header and footer elements. You can also leverage any of the available ASP.NET controls in various parts of the FormView control.
The syntax for
declaring and using a FormView control instance is very similar to declaring and using a DetailsView control instance. The main difference between them is that, because there are no default settings to use, you must include the format and template for displaying the data in the FormView control. Listing A shows part of the syntax for opening a FormView element tag.
You may have noticed that many attributes correspond to HTML table elements, such as titles and borders. This shows that ASP.NET uses HTML tables to render the FormView control.
You can view a more comprehensive list of FormView control properties online through the Microsoft website. The table below lists some important properties to look out for.
·AllowPaging: A Boolean value indicating whether the user can page the records in the specified data source. If set to true, displays the default paginated number system (from 1 to the number of records) at the bottom of the displayed records. Pagination links can be customized through various pagination properties.
·DataKeyNames: Key fields of the data source.
·DataSourceID: used to transplant the FormView control data source element ID. If using SQL Server, this corresponds to the ID assigned to the SqlDataSource element.
·DefaultMode: Allows you to specify the default behavior of the control. That is, how it is initially displayed when the user accesses it. Possible values include: ReadOnly, Insert, and Edit.
·EmptyDataText: The text displayed when an empty data value is encountered.
When you declare a FormView control, its content must also be formatted accordingly. Its data is displayed through templates. The FormView control mainly uses five templates:
·ItemTemplate: It controls the display when users view data.
·EditItemTemplate: It determines the format and display of data elements when users edit records. Within this template, you will use other controls, such as TextBox elements, to allow users to edit values.
·InsertItemTemplate: Similar to editing a record, this template controls the display of fields that allow the user to add a new record in the backend data source. As new values are entered, the user should be allowed to freely enter text or restricted to certain values, depending on the requirements of the data.
·FooterTemplate: Determines the content displayed in the footer part of the FormView control table, if any.
·HeaderTemplate: Determines the content displayed in the header part of the FormView control table, if any.
These templates allow you to control the display and behavior of data bound to a FormView control. For example, the ASP.NET web form in Listing B connects to a standard Northwind database and allows users to view, edit, delete, and add new employee records via the first name, last name, hire date, and home phone number fields.
It uses the TextField control to display data being edited or added, as well as values that are displayed just for review. ItemTemplate uses CSS to format tables, while InsertTemplate uses HTML styles for formatting. Which method to use is decided by the developer.
Note: ASP.NET uses Button controls to add, edit, delete and save records.
In the Button control, New's CommandName value converts the record into insert mode and loads the InsertItemTemplate template, which allows the user to enter a new record value. You can use the Edit CommandName value to add a new button to the ItemTemplate to put the FormView control into edit mode.
You can add a button with a CommnadName value of Delete to the ItemTemplate template to allow the user to delete the current record from the data source. Update's CommnadName saves the data, while Cancel terminates the operation.
It's amazing how easy it is for
developers to control
many of the new ASP.NET 2.0 features.FormView expands the simple functionality of DetailsView, allowing you to easily control the display content to be formatted as needed. This new control gives you another option for delivering your solutions.