以编程方式在 Asp.Net ListView 中选择项目 [英] Programmatically Select Item in Asp.Net ListView
问题描述
在快速搜索之后,我找不到这个看似简单的事情的答案.
After doing a quick search I can't find the answer to this seemingly simple thing to do.
如何在 Asp.Net ListView 中手动选择项目?
我有一个 SelectedItemTemplate,但我不想使用 asp:button 或 asp:LinkButton 来选择项目.我希望它是从一个 URL 完成的.例如,像 QueryString.
I have a SelectedItemTemplate, but I don't want to use an asp:button or asp:LinkButton to select an item. I want it to be done from a URL. Like a QueryString, for example.
我想象的方式是在 ItemDataBound 上,检查一个条件,然后将其设置为 selected(如果为真),但我该怎么做?
The way I imagine would be on ItemDataBound, check a condition and then set it to selected if true, but how do I do this?
例如:
protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e) {
using (ListViewDataItem dataItem = (ListViewDataItem)e.Item) {
if (dataItem != null) {
if( /* item select condition */ ) {
// What do I do here to Set this Item to be Selected?
// edit: Here's the solution I'm using :
((ListView)sender).SelectedIndex = dataItem.DisplayIndex;
// Note, I get here and it gets set
// but the SelectedItemTemplate isn't applied!!!
}
}
}
}
我确定这是一两行代码.
I'm sure it's one or two lines of code.
我已经更新了代码以反映解决方案,似乎我可以选择 ListView 的 SelectedItemIndex,但是,它实际上并没有呈现 SelectedItemTemplate.我不知道我是否应该按照下面的建议在 ItemDataBound 事件中执行此操作.
I've updated the code to reflect the solution, and it seems that I can select the ListView's SelectedItemIndex, however, it's not actually rendering the SelectedItemTemplate. I don't know if I should be doing this in the ItemDataBound event as suggested below.
推荐答案
我查看了 ListView 内部发生的一些事情,并认为这可能是最好的方法.
I looked at some of what's going on in ListView under the hood and think this is probably the best approach.
void listView_ItemCreated(object sender, ListViewItemEventArgs e)
{
// exit if we have already selected an item; This is mainly helpful for
// postbacks, and will also serve to stop processing once we've found our
// key; Optionally we could remove the ItemCreated event from the ListView
// here instead of just returning.
if ( listView.SelectedIndex > -1 ) return;
ListViewDataItem item = e.Item as ListViewDataItem;
// check to see if the item is the one we want to select (arbitrary) just return true if you want it selected
if (DoSelectDataItem(item)==true)
{
// setting the SelectedIndex is all we really need to do unless
// we want to change the template the item will use to render;
listView.SelectedIndex = item.DisplayIndex;
if ( listView.SelectedItemTemplate != null )
{
// Unfortunately ListView has already a selected a template to use;
// so clear that out
e.Item.Controls.Clear();
// intantiate the SelectedItemTemplate in our item;
// ListView will DataBind it for us later after ItemCreated has finished!
listView.SelectedItemTemplate.InstantiateIn(e.Item);
}
}
}
bool DoSelectDataItem(ListViewDataItem item)
{
return item.DisplayIndex == 0; // selects the first item in the list (this is just an example after all; keeping it simple :D )
}
注意事项
- ListView 选择项目在触发 DataBinding 事件后将使用的模板.所以如果在此之前设置了 SelectedIndex ,则不需要更多的工作
- 在 DataBinding 工作后的任何位置设置 SelectedIndex,您只是不会获得 SelectedItemTemplate.为此,您要么重新绑定数据;或重新实例化 ListViewItem 上的 SelectedItemTemplate.请务必先清除 ListViewItem.Controls 集合!
更新我已经删除了我的大部分原始解决方案,因为这应该会更好地适用于更多情况.
UPDATE I have removed most of my original solution, since this should work better and for more cases.
这篇关于以编程方式在 Asp.Net ListView 中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!