简体   繁体   English

为什么我的ListView SelectedItemTemplate不被显示?

[英]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? 有一个更好的方法吗?

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!" “在数据绑定工作之后,在任何地方设置SelectedIndex,您只是没有得到SelectedItemTemplate。为此,您要么重新绑定数据;要么重新实例化ListViewItem上的SelectedItemTemplate。请确保首先清除ListViewItem.Controls集合!”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么默认情况下我的winform列表视图项不显示? - Why don't my winform listview items show up selected by default? 为什么我的RelayCommand不会触发并且ObservableCollection捕获所选值? - Why won't my RelayCommand fire and ObservableCollection catch selected value? 为什么我的数据绑定WPF ListBox不显示行? - Why won't my databound WPF ListBox show rows? 为什么 Visual Studios 不显示我的 Unity 脚本? - Why won't Visual Studios show me my Unity script? 为什么我的ListView没有正确显示详细信息视图? - Why doesn't my ListView show Details view properly? 为什么我的 ListView 数据绑定到可观察集合没有正确显示 - Why my ListView databinding to observable collection don't show correctly 为什么当鼠标悬停在ScrollViewer上时,为什么我的WPF样式触发器无法仅显示水平滚动条? - Why won't my WPF style trigger work to only show the horizontal scroll bar when the mouse is over my ScrollViewer? 在TabItem中不会显示ListView - ListView won't show up while in TabItem 我的RadioButtonList不会在回发后追加查询字符串或显示选定的值-C#和.Net - My RadioButtonList won't append Query String or show selected value after Postback - C# & .Net 为什么将我的控件以编程方式添加到选项卡时不显示 - Why won't my controls display when added to a tab programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM