Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [259]

By Root 5731 0
Both ways are widely accepted. The class approach makes more obvious that something has been styled and what class it has been assigned to. But, in the end, it’s a matter of preference. If you opt for styling via IDs, you are totally free to choose any names you want. (Note, however, that IDs must be unique to allow them to be used with client scripts. This can be hard to achieve with multiple controls in one page, so, class names are really preferred.)

DataSourceID="ObjectDataSource1"

ItemPlaceholderID="ListViewContent">

CompanyCountry

Text='<%# Eval("CompanyName") %>' />

Text='<%# Eval("Country") %>' />

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
NameText='<%# Bind("CompanyName") %>' />
CountryText='<%# Bind("Country") %>' />
StreetText='<%# Bind("Street") %>' />
CityText='<%# Bind("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

Return Main Page Previous Page Next Page

®Online Book Reader