|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
How to: Allow Users to Select Items in DataList Web Server Controls
You can specify that users can select individual items in theWeb server control. Typically, selecting an item highlights it visually. In addition, you might display different information for a selected item.
To allow users to select items in a DataList control
Create ato define the layout of markup and controls for a selected item. For details, see .
Set the control'sproperties. For details, see .
In the ItemTemplate (and AlternatingItemTemplate, if you are using it), add a Button or LinkButton Web server control.
Set the CommandName property of the button from Step 3 to select (case-sensitive).
Create an event handler for the DataList control'sevent. In the event handler, call the control's DataBind method to refresh the information in the control. The complete code would look something like this:
Protected Sub DataList1_SelectedIndexChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles DataList1.SelectedIndexChanged DataList1.DataBind() End Sub
To cancel the selection, set the control's SelectedIndex property to -1. To accomplish this, you could add a Button Web server control to the SelectedItem template and set its CommandName property to "unselect". The Click event for this button would also be forwarded to the DataGrid control's ItemCommand event.
The complete code might look something like the following: