快速搜索之后,我找不到这个看似简单的事情的答案。
如何在Asp.Net ListView中手动选择项目?
我有一个SelectedItemTemplate,但我不想使用asp:button或asp:LinkButton来选择一个项目。我希望它可以通过URL完成。例如,像QueryString一样。
我想象的方式是在ItemDataBound上,检查条件然后将其设置为selected,如果为true,但是我该怎么做?
例如:
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!!!
}
}
}
}
我确定这是一行或两行代码。
编辑:我已经更新了代码以反映解决方案,似乎我可以选择ListView的SelectedItemIndex,但是,它实际上并没有呈现SelectedItemTemplate。我不知道我是否应该在ItemDataBound事件中执行此操作,如下所示。
答案 0 :(得分:13)
我看了一下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 )
}
备注强>
更新我删除了原始解决方案的大部分内容,因为这样可以更好地处理更多情况。
答案 1 :(得分:5)
您可以设置ListViews SelectedIndex
list.SelectedIndex = dataItem.DisplayIndex; // don't know which index you need
list.SelectedIndex = dataItem.DataItemIndex;
如果在页面加载时加载数据,则可能必须遍历数据以查找索引,然后在调用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;
}
}
答案 2 :(得分:3)
list.SelectedIndex = list.Items.IndexOf(item);
答案 3 :(得分:2)
扩展@Jeremy和@ bendewey的答案,你不需要在ItemDataBound中这样做。在设置SelectedValue之前,您只需要已经发生了ListView绑定。您应该能够在PreRender期间执行此操作。有关何时进行绑定的详细信息,请参阅此页生命周期docs。