title: Programmatically Select Item in Asp.Net ListView
body: <p>After doing a quick search I can't find the answer to this seemingly simple thing to do.</p>
  
  <p><strong>How do I Manually Select An Item in an Asp.Net ListView?</strong> </p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>For example:</strong></p>
  
  <pre><code>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></pre>
  
  <p>I'm sure it's one or two lines of code.</p>
  
  <p><strong>EDIT:</strong> 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 <strong>as suggested below</strong>.</p>