[英]Why won't my ListView SelectedItemTemplate show when selected?
I have a databound ListView in asp.Net. 我在asp.Net中有一个数据绑定的ListView。 I have both ItemTemplate and SelectedItemTemplate used in the aspx page.
我在aspx页面中同时使用了ItemTemplate和SelectedItemTemplate。
In the .cs page I have this. 在.cs页面中,我有这个。 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.
我已经通过逐步验证以下代码进行验证:if语句仅在应该假定为真时才评估为true,并且已设置选定的索引。 However the HTML output is as if all items use the ItemTemplate.
但是,HTML输出就像所有项目都使用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? 有一个更好的方法吗?
"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!" “在数据绑定工作之后,在任何地方设置SelectedIndex,您只是没有得到SelectedItemTemplate。为此,您要么重新绑定数据;要么重新实例化ListViewItem上的SelectedItemTemplate。请确保首先清除ListViewItem.Controls集合!”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.