在Asp.Net ListView控件编程方式选择项 [英] Programmatically Select Item in Asp.Net ListView

查看:193
本文介绍了在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:按钮或ASP:LinkBut​​ton的选择项目。我希望它从一个URL来完成。像查询字符串,例如

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的方式,检查条件,然后将其设置为如果属实选择,但我怎么做到这一点?

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!!!

        }
     }
  }
}

我敢肯定,这是code的一行或两行。

I'm sure it's one or two lines of code.

编辑:我已经更新了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选择它的数据绑定事件触发后的项目将使用的模板。所以,如果selectedIndex为前接盘,没有更多的工作是必要

  • 设置的SelectedIndex数据绑定工作后的任何地方,你只是没有得到SelectedItemTemplate。对于你或者重新绑定的数据;重新实例或在ListViewItem的的SelectedItemTemplate。 请务必先清除ListViewItem.Controls集合!

  • 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!

更新我已删除了我的大部分原来的解决方案,因为这应该工作更多的情况下,更好。

UPDATE I have removed most of my original solution, since this should work better and for more cases.

这篇关于在Asp.Net ListView控件编程方式选择项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆