Online Book Reader

Home Category

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

By Root 5631 0
and it’s enabled on the last-clicked item.

To turn the ListView user interface into edit mode, you need an ad hoc button control with a command name of Edit:

When this button is clicked, the ItemEditing event fires on the server. By handling this event, you can run your own checks to ensure that the operation is legitimate. If something comes up to invalidate the call, you set the Cancel property of the event data structure to cancel the operation, like so:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)

{

// Just deny the edit operation

e.Cancel = true;

}

Adding Predefined Command Buttons


An edit item template wouldn’t be very helpful without at least a couple of predefined buttons to save and cancel changes. You can define buttons using a variety of controls, including Button, LinkButton, ImageButton, and any kind of custom control that implements the IButtonControl interface.

Command names are plain strings that can be assigned to the CommandName property of button controls. The ListView (and other view controls) recognizes a number of predefined command names, as listed in Table 11-5.

Table 11-5. Supported Command Names

Command

Description

Cancel

Cancels the current operation (edit, insert), and returns to the default view (item template)

Delete

Deletes the current record from the data source

Edit

Turns the ListView control into edit mode (edit item template)

Insert

Inserts a new record into the data source

Page

Moves to the next or previous page

Select

Selects the clicked item, and switches to the selected item template

Sort

Sorts the bound data source

Update

Saves the current status of the record back to the data source

The following code shows how to add a pair of Save/Cancel buttons:

Any button clicking done within the context of a ListView control originates a server-side event—the ItemCommand event:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)

{

// Use e.CommandName to check the command requested

}

Clicking buttons associated with predefined command buttons can result in subsequent, and more specific, events. For example, ItemUpdating and ItemUpdated are fired before and after, respectively, a record is updated. You can use the ItemUpdating event to make any last-minute check on the typed data before this data is sent to the database.

Note that before the update is made, ListView checks the validity of any data typed by calling the IsValid method on the Page class. If any validator is defined in the template, it is evaluated at this time.

Adding Custom Command Buttons


In the edit mode user interface, you can have custom buttons too. A custom button differs from a regular Save or Cancel button only in terms of the command name. The command name of a custom button is any name not listed in Table 11-5. Here’s an example:

CommandName="mycommand" />

To execute any code in response to the user’s clicking on this button, all you can do is add an ItemCommand event handler and check for the proper (custom) command name and react accordingly:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)

{

// Check the command requested

if (e.CommandName == "MyCommand")

{

...

}

}

Conducting the Update


When the ListView control is used in two-way binding mode, any update operation is conducted through the connected data source control. You define select and save methods on the data source, configure their parameters (either declaratively or programmatically), and delegate to the ListView control all remaining chores.

For update and delete operations, though, you need to identify the record uniquely. This is where the DataKeyNames property gets into the game.

Return Main Page Previous Page Next Page

®Online Book Reader