Online Book Reader

Home Category

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

By Root 5241 0
/>

How would you display this template? The edit item template shows up when the user clicks a button decorated with the Edit command name. Unfortunately, there’s no equivalent New command name to automatically bring up the insert item template. Instead, with the ListView the New command name is considered a custom command, handled by code you provide to activate the insert item template, unless it’s active by default. We’ll look at the details next.

The insert item template is displayed by position. The InsertItemPosition property determines where the template is displayed. There are three possibilities, as shown in Table 11-6.

Table 11-6. Feasible Positions for the Insert Item Template

Position

Description

FirstItem

The insert item template is displayed as the first item in the list and precedes all items in the bound data source.

LastItem

The insert item template is displayed as the last item in the list and trails all items in the bound data source.

None

The insert item template is not automatically displayed. The developer is responsible for showing and hiding the template programmatically. This is the default value for the InsertItemPosition property.

If you leave the InsertItemPosition property set to its default value, no insert template is displayed, but you won’t have a predefined button to bring it up. If you use any of the other two values, the template is always visible and displayed at the beginning or the end of the list. This might not be desirable in most cases. Let’s see how to take programmatic control over the display of the insert template.

Taking Full Control of the Insert Template


In the layout template, you add a custom button and capture any user’s click event. You can give the button any command name not listed in Table 11-5:

To handle the click on the button, you write an ItemCommand handler. In the event handler, you simply change the value of the InsertItemPosition property, as shown here:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)

{

if (e.CommandName.Equals("New", StringComparison.OrdinalIgnoreCase))

{

ListView me = (ListView) sender;

me.InsertItemPosition = InsertItemPosition.FirstItem;

}

}

Changing the value of InsertItemPosition to anything but None brings up the insert item template, if any. In the insert template, you need to have a couple of predefined buttons with command names of Insert (to add) and Cancel (to abort).

It should be noted, though, that the insert item template is not automatically dismissed by the ListView control itself. As mentioned, this is because of the lack of built-in support for the New command name. In the end, this requires that you add a couple more handlers to dismiss the template when the user cancels or confirms the insertion.

The ItemCanceling event fires when the user hits a button associated with the Cancel command name. This can happen from either the edit or insert template. The event data object passed to the handler has the CancelMode property, which is designed to help you figure out what mode is active (insert or edit) and allow you to tailor your application’s response.

protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)

{

ListView me = (ListView) sender;

// Dismissing the insert item template

if (e.CancelMode == ListViewCancelMode.CancelingInsert)

me.InsertItemPosition = InsertItemPosition.None;

}

To hide the insert item template after the new data item has been successfully appended to the data source, you use the ItemInserted event:

protected void ListView1_ItemInserted(object sender, ListViewInsertedEventArgs e)

{

ListView me = (ListView) sender;

me.InsertItemPosition = InsertItemPosition.None;

}

Adding a Bit of Validation


When you’re going to add a new record to an existing data source, a bit of validation—much more than is

Return Main Page Previous Page Next Page

®Online Book Reader