Reputation: 25258
I have a databound ListView in asp.Net. I have both ItemTemplate and SelectedItemTemplate used in the aspx page.
In the .cs page I have this. I have verified by stepping through the code that the if statement evaluates to true only when it is supposed to, and that the selected index is set. However the HTML output is as if all items use the ItemTemplate.
int indexCounter = 0;
protected void lvProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
if (((DataRowView)e.Item.DataItem)["ID"].ToString() == Request.QueryString["id"])
{
lvProducts.SelectedIndex = indexCounter;
}
indexCounter++;
}
}
Is there a better way to do this?
Upvotes: 0
Views: 2432
Reputation: 5264
check out this post
"Setting the SelectedIndex anywhere after DataBinding works, you just don't get the SelectedItemTemplate. For that you have either rebind the data; or reinstantiate the SelectedItemTemplate on the ListViewItem. be sure to clear the ListViewItem.Controls collection first!"
Upvotes: 3