Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


programmatically select item in asp net listview


programmatically select item in asp net listview  using -'c#,asp.net,listview,listviewitem,selecteditemtemplate'

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?

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.

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?

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.
    

asked Sep 9, 2015 by AmeliaQ58ixt
0 votes
45 views



Related Hot Questions



Government Jobs Opening


...