快速搜索后,我找不到这个看似简单的事情的答案.
如何在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事件中执行此操作,如下所示.
我看了一下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 ) }
笔记
ListView选择一个项目在DataBinding事件触发后将使用的模板.因此,如果在此之前设置了SelectedIndex,则无需再进行任何操作
在DataBinding工作之后的任何地方设置SelectedIndex,你只是没有得到SelectedItemTemplate.为此你要么重新绑定数据; 或者在ListViewItem上重新实例化SelectedItemTemplate.一定要先清除ListViewItem.Controls集合!
更新我删除了大部分原始解决方案,因为这应该更好,更多情况下.
您可以设置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(this IEnumerable source, Predicate predicate) { int count = 0; foreach(var item in source) { if (predicate(item)) return count; count++; } return -1; } }