| Company | Country |
|---|
Programming Microsoft ASP.NET 4 - Dino Esposito [259]
ItemPlaceholderID="ListViewContent">Customer List
The result is shown in Figure 11-9.
Figure 11-9. Using cascading style sheets to style the markup of a ListView control.
Working with the ListView Control
The ListView control makes it easy to handle common data-based operations, such as insert, update, delete, or sorting. All that you have to do is place buttons in the layout template and associate buttons with command names. Buttons can be global to the list (such as insert, sort, and page buttons) or specific to a particular item (such as update and delete buttons). Command names are just strings that are assigned to the CommandName property of the Button control.
So far, we have considered only scenarios with relatively static and noninteractive templates. It is definitely possible, though, to use the ListView control to create rich user interfaces that allow in-place editing, selection of items, paging, and updates back to the data source. Let’s start with in-place editing.
In-Place Editing
Unlike the GridView control, the ListView control doesn’t automatically generate an Edit button; nor does it automatically adapt the edit mode user interface from the item template.
This responsibility falls to the developer by design. The developer is required to define an edit template that will be used to edit the contents of the selected item, in keeping with the flexible nature of the control.
Defining the Edit Item Template
The edit template is any piece of markup you intend to display to your users when they click to edit a record. It can have any layout you like and can handle data access in a variety of ways.
If you’ve bound the ListView control to a data source control—for example, an ObjectDataSource control—you can take advantage of the ASP.NET built-in support for two-way data binding. Simply put, you use data binding <%# … %> expressions to bind to data, the Eval method for read-only operations, and the Bind method for full I/O operations.
The following markup defines a classic two-column table for editing some fields of a customer record:
| ID | |
| Name | |
| Country | |
| Street | |
| City |
Only one displayed item at a time can be in edit mode; the EditIndex property is used to get or set this 0-based index. If an item is being edited and the user clicks on a button to edit another one, the last-win policy applies. As a result, editing on the previous item is canceled