Why won't my ListView SelectedItemTemplate show when selected?
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开发者_如何学Go 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?
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!"
精彩评论