C# 以编程方式选择Asp.Net ListView中的项

C# 以编程方式选择Asp.Net ListView中的项,c#,asp.net,listview,listviewitem,selecteditemtemplate,C#,Asp.net,Listview,Listviewitem,Selecteditemtemplate,在快速搜索之后,我找不到这个看似简单的问题的答案 如何在Asp.Net ListView中手动选择项目? 我有一个SelectedItemTemplate,但我不想使用asp:button或asp:LinkButton来选择项目。我希望它是从一个网址。例如,像查询字符串 我想象的方式是在ItemDataBound上,检查一个条件,然后将其设置为selected(如果为true),但是我如何做到这一点呢 例如: protected void lv_ItemDataBound(object send

在快速搜索之后,我找不到这个看似简单的问题的答案

如何在Asp.Net ListView中手动选择项目?

我有一个SelectedItemTemplate,但我不想使用asp:button或asp:LinkButton来选择项目。我希望它是从一个网址。例如,像查询字符串

我想象的方式是在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的SelectEditedIndex,但是,它实际上并没有呈现SelectedItemTemplate。我不知道是否应该在下面建议的ItemDataBound事件中执行此操作

list.SelectedIndex = list.Items.IndexOf(item);
list.SelectedIndex = dataItem.DisplayIndex; // don't know which index you need
list.SelectedIndex = dataItem.DataItemIndex; 
更新 如果在页面加载时加载数据,则可能必须遍历数据以查找索引,然后在调用DataBind()方法之前设置SelectedIndex值

public void页面加载(对象发送方,事件参数e)
{
var myData=MyDataSource.GetPeople();
list.DataSource=myData;
list.SelectedIndex=myData.FirstIndexOf(p=>p.Name.Equals(“Bob”,StringComparison.InvariantCultureInoRecase));
list.DataBind();
}
公共静态类EnumerableExtensions
{
公共静态int FirstIndexOf(此IEnumerable源,谓词)
{
整数计数=0;
foreach(源中的var项)
{
if(谓语(项))
返回计数;
计数++;
}
返回-1;
}
}

扩展@Jeremy和@bendewey的答案,您不需要在ItemDataBound中执行此操作。您只需要在设置SelectedValue之前已进行ListView绑定。您应该能够在预渲染期间执行此操作。有关何时进行绑定的更多信息,请参见本页“生命周期”。

我查看了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选择触发数据绑定事件后项目将使用的模板。因此,如果在此之前已设置SelectedIndex,则无需进行更多工作
  • 在数据绑定后的任何位置设置SelectedIndex都会起作用,只是无法获得SelectedItemTemplate。为此,您可以重新绑定数据;或者在ListViewItem上重新实例化SelectedItemTemplate。确保清除ListViewItem。首先控制集合

更新我已删除了大部分原始解决方案,因为这将更好地适用于更多情况。

在预渲染中执行此操作有好处吗?我通常在ItemDataBound中做这类事情。我还在这里设置一个asp:HyperLink的URL。还有一条评论。我这样设置:((ListView)sender);但是它呈现标准的ItemTemplate。这依赖于ViewState还是应该使用PreRender?我现在会浏览生命周期文档。另外,如果你需要生命周期,请将此图表挂在你的立方体墙上,上面的参考URL对我不起作用。你可以从贾斯汀的博客下载,这里:谢谢。我正在经历,谢谢!第二种解决方案似乎不起作用:list.SelectedValue=dataItem;(list.SelectedValue为只读)。使用DisplayIndex似乎可行,但不会更改项目的模板。我像这样使用发送者:((ListView)sender);似乎您必须在绑定之前设置SelectedIndex?如何以及在何处设置ListView数据源?您正在使用数据源控件吗?您是否以编程方式设置数据源并调用DataBind?我正在PageLoad中设置数据源。对我的数据层的一个简单调用,在那里我返回一个列表。然后我调用DAtaBind()。没有用于更新的DataSourceControlThanks。这是一个很好的解决方案,看起来很有效。我将使用所选的解决方案,只是因为它将代码封装到ItemCreated方法中,这是我更喜欢的方法,但是谢谢!当我在ItemDataBound中设置SelectedIndex时,它似乎不需要。这依赖于ViewState吗?谢谢!我要试试这个。这似乎是最有希望的解决方案。哇!感谢您提供的所有文档。棒极了!重新实例化SelectedItemTemplate的非常优雅的解决方案。
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 )
}