Items
Selecting items
RadListView exposes a server-side API, similar to RadGrid's. The selected items are accessible through the SelectedItems collection that consist of RadListViewDataItem objects.
By default you can select only one item at a time. Multiple selection is possible if enabled via the AllowMultiItemSelection property.
There are several ways to select/deselect an item in RadListView:
-
to use the Selected property of RadListViewDataItem
-
to fire Select/Deselect command
-
to add/remove item's index to the SelectedIndexes collection
Note, that you need to provide Select/Deselect buttons in both Item and AlternatingItem templates.
<itemtemplate>
<asp:LinkButton ID="SelectButton1" Text="Select" CommandName="Select" runat="server" />
<asp:LinkButton ID="DeselectButton1" Text="Deselect" CommandName="Deselect" runat="server" />
...
</itemtemplate>
<alternatingitemtemplate>
<asp:LinkButton ID="SelectButton2" Text="Select" CommandName="Select" runat="server" />
<asp:LinkButton ID="DeselectButton2" Text="Deselect" CommandName="Deselect" runat="server" />
...
</alternatingitemtemplate>
The selected items can be cleared using the ClearSelectedItems method.
Brief description of the selection-related properties/methods:
Property/Method | Type | Description |
---|---|---|
SelectCommandName (Select) | Command / constant | Selects RadListView item |
DeselectCommandName (Deselect) | Command / constant | Deselects RadListView item |
AllowMultiItemSelection | Property ( bool ) | Allows the user to select more that one item simultaneously |
SelectedIndexes | Property ( RadListViewIndexesCollection ) | Contains the indexes of the selected items |
SelectedItems | Property ( RadListViewDataItemCollection ) | Contains the selected items |
SelectedValue | Property ( object ) | Gets the first data key value of the selected item |
SelectedValues | Property ( DataKey ) | Gets the data key values from the selected item |
SelectedIndexChanged | Event | This event fires when the user selects an item in RadListView |
ClearSelectedItems | Method | Clears the selected items |
SelectedItemTemplate
SelectedItemTemplate defines the contents that represents the selected item in RadListView. As with any other template, SelectedItemTemplate provides an unrestricted freedom for defining the appearance of the selected item.
A simplified code-snippet is shown below:
<telerik:radlistview id="RadListView1" runat="server">
...
<ItemTemplate>
<asp:Label ID="lbl1" Text='<%# Bind("UserName") %>' runat="server"> </asp:Label>
</ItemTemplate>
<SelectedItemTemplate>
<asp:Label ID="lbl2" Text='<%# Bind("UserName") %>' runat="server" BackColor="Gray"> </asp:Label>
</SelectedItemTemplate>
...</telerik:radlistview>
Extracting values from a selected item
There are various way to access/extract values from the items in RadListView.
- Extracting DataKey values on ItemDataBound / ItemCommand events
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item is RadListViewDataItem)
{
RadListViewDataItem item = e.Item as RadListViewDataItem;
if (item.Selected)
{
string myValue = item.GetDataKeyValue("myValue").ToString();
}
}
}
protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
{
if (e.CommandName == RadListView.SelectCommandName)
{
RadListViewDataItem item = e.ListViewItem as RadListViewDataItem;
string myValue = item.GetDataKeyValue("myValue").ToString();
}
}
- Extracting values using the ExtractValues method on ItemDataBound / ItemCommand
ExtractValues method will only work when Editing and/or Selecting an item. Instead, extract the values through the DataItem following the example provided in the 4th point of this article.
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item is RadListViewDataItem)
{
RadListViewDataItem item = e.Item as RadListViewDataItem;
if (item.Selected)
{
Hashtable table = new Hashtable();
(e.Item as RadListViewDataItem).ExtractValues(table);
string myValue = table["myValue"].ToString();
}
}
}
protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
{
if (e.CommandName == RadListView.SelectCommandName)
{
Hashtable table = new Hashtable();
(e.ListViewItem as RadListViewDataItem).ExtractValues(table);
string myValue = table["myValue"].ToString();
}
}
- Extracting values using the ExtractValuesFromItem method on ItemDataBound / ItemCommand
Similar to the previous example but uses ExtractValuesFromItem. A major advantage of this method is that it can also extract the DataKeys values. This also applies for items that are being Edited and/or Selected.
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item is RadListViewDataItem)
{
RadListViewDataItem item = e.Item as RadListViewDataItem;
if (item.Selected)
{
Hashtable table = new Hashtable();
e.Item.OwnerListView.ExtractValuesFromItem(table, e.Item as RadListViewDataItem, true);
string myValue = table["myValue"].ToString();
}
}
}
- Extracting values through the DataItem on ItemDataBound
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item is RadListViewDataItem)
{
RadListViewDataItem item = e.Item as RadListViewDataItem;
if (item.Selected)
{
string myValue = DataBinder.Eval(item.DataItem, "myValue").ToString();
}
}
}
Online demo: