C# 以编程方式在 Asp.Net ListView 中选择项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Select Item in Asp.Net ListView
提问by Armstrongest
After doing a quick search I can't find the answer to this seemingly simple thing to do.
快速搜索后,我找不到这个看似简单的事情的答案。
How do I Manually Select An Item in an Asp.Net ListView?
如何在 Asp.Net ListView 中手动选择项目?
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.
我有一个 SelectedItemTemplate,但我不想使用 asp:button 或 asp:LinkButton 来选择一个项目。我希望它是从一个 URL 完成的。例如,像 QueryString。
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?
我想象的方式是在 ItemDataBound 上,检查一个条件,然后将其设置为 selected 如果为真,但我该怎么做?
For example:
例如:
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.
我确定这是一两行代码。
EDIT: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 的 SelectedItemIndex,但是,它实际上并没有呈现 SelectedItemTemplate。我不知道我是否应该按照下面的建议在 ItemDataBound 事件中执行此操作。
采纳答案by Joshua Shannon
I looked at some of what's going on in ListView under the hood and think this is probably the best approach.
我在后台查看了 ListView 中发生的一些事情,并认为这可能是最好的方法。
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 )
}
NOTES
笔记
- ListView selects the template an item will use after it's DataBinding event fires. So if the SelectedIndex is set before then, no more work is necessary
- 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!
- ListView 选择项目在触发 DataBinding 事件后将使用的模板。所以如果 SelectedIndex 在此之前设置,则不需要更多工作
- 在 DataBinding 工作后的任何地方设置 SelectedIndex,您只是不会获得 SelectedItemTemplate。为此,您要么重新绑定数据;或重新实例化 ListViewItem 上的 SelectedItemTemplate。一定要先清除 ListViewItem.Controls 集合!
UPDATEI have removed most of my original solution, since this should work better and for more cases.
更新我已经删除了我的大部分原始解决方案,因为这应该可以更好地工作并且适用于更多情况。
回答by jlew
list.SelectedIndex = list.Items.IndexOf(item);
回答by bendewey
You can set the ListViews SelectedIndex
您可以设置 ListViews SelectedIndex
list.SelectedIndex = dataItem.DisplayIndex; // don't know which index you need
list.SelectedIndex = dataItem.DataItemIndex;
Update
更新
If your loading the data on page load you may have to traverse the data to find the index then set the SelectedIndex value before calling the DataBind() method.
如果您在页面加载时加载数据,您可能必须遍历数据以找到索引,然后在调用 DataBind() 方法之前设置 SelectedIndex 值。
public void Page_Load(object sender, EventArgs e)
{
var myData = MyDataSource.GetPeople();
list.DataSource = myData;
list.SelectedIndex = myData.FirstIndexOf(p => p.Name.Equals("Bob", StringComparison.InvariantCultureIgnoreCase));
list.DataBind();
}
public static class EnumerableExtensions
{
public static int FirstIndexOf<T>(this IEnumerable<T> source, Predicate<T> predicate)
{
int count = 0;
foreach(var item in source)
{
if (predicate(item))
return count;
count++;
}
return -1;
}
}
回答by tvanfosson
Expanding on @Jeremy and @bendewey's answers, you shouldn't need to do this in ItemDataBound. You only need to have the ListView binding already have taken place before you set the SelectedValue. You should be able to do this during PreRender. See this page life cycle docsfor more information on when binding takes place.
扩展@Jeremy 和@bendewey 的答案,您不需要在 ItemDataBound 中执行此操作。您只需要在设置 SelectedValue 之前已经进行了 ListView 绑定。您应该能够在 PreRender 期间执行此操作。有关何时发生绑定的更多信息,请参阅此页面生命周期文档。