Online Book Reader

Home Category

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

By Root 5715 0

In the item template, though, you need to insert a button control to trigger the selection process. You can use a push button or attach any significant text in the template to a link button:

Figure 11-11 shows the result.

Figure 11-11. A selected item in a ListView control.

Releasing the Selection


When you click the link button, the ListView switches the template and sets the SelectedIndex property accordingly. As soon as the user clicks on a different item, the selection is moved and the previously selected item regains the regular template. Is there a way to programmatically reset the selection? You bet.

All that you have to do is add a new custom button and handle its click event. In the event handler, you assign the –1 value to the SelectedIndex property. A value of –1 means that no items are selected. Here’s the related code snippet:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)

{

ListView me = (ListView) sender;

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

me.SelectedIndex = -1;

}

Note that the index of the currently selected item and the index of the item being edited are saved to the view state and persisted across postbacks. This means that if the user changes the country/region selection (shown in Figure 11-11), both the edit and selection indexes are retained, which might not be desirable. For example, imagine that you selected (or are editing) the second customer from Argentina. Next, the user changes to Brazil while the selected (or edit) template is on. The result is that the second customer from Brazil is displayed in the selected (or edit) mode. If this behavior works for you, there’s nothing to modify in the code. Otherwise, you need to reset SelectedIndex and EditIndex in any postback event outside the ListView control. Here’s an example:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

// The sender argument here indicates the DropDownList or any other

// control responsible for the postback. You reference the ListView by

// name or via a custom global member in the code-behind class of the page

ListView1.SelectedIndex = -1;

ListView1.EditIndex = -1;

}

Paging the List of Items


In ASP.NET, grid controls support data paging natively. Purely iterative controls such as Repeater and DataList, though, leave the burden of pagination entirely on the developer’s capable shoulders. The ListView control falls somewhere in the middle of these two extreme positions. The ListView control doesn’t have built-in paging capabilities, but it knows how to work with a new control specifically designed to enable data paging on a variety of data-bound controls. This control is the DataPager.

The DataPager Control


The DataPager control is designed to add paging capabilities to a family of data-bound controls and not just the ListView. The support that the DataPager control offers to data-bound pageable controls such as the ListView is limited to the user interface of the pager.

You configure the DataPager to obtain the pager bar you like best, and then you instruct the DataPager control to fall back to the paged control to display the specified number of data items starting at the specified position. In no case does the DataPager expose itself to the data source or a data source control. All that it does is communicate to the paged control the next set of records to select and display. Table 11-7 lists the properties of the DataPager control.

Table 11-7. Properties of the DataPager Control

Property

Description

Fields

Gets the collection of DataPagerField elements that form the pager bar. Elements in this collection belong to classes such as NumericPagerField, TemplatePagerField, and NextPreviousPagerField.

MaximumRows

Gets the maximum number of rows that the page can support.

PagedControlID

Gets and sets the ID of the control to page. This control must implement the IPageableItemContainer

Return Main Page Previous Page Next Page

®Online Book Reader