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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…