2

Question basically crams it all in... I'm loading a page with a querystring (ID), and I need to use that ID to set the selected item of a ListView when the page loads. The ID is a DataKey on the ListView. Please help!

I have no code of value to post--none of my attempts at this work.

zk812
  • 81
  • 10
  • Take a look at this Q - http://stackoverflow.com/questions/570801/programmatically-select-item-in-asp-net-listview – RPM1984 Oct 06 '10 at 21:32
  • @RPM1984 - That's got me closer than I've been yet, but I still have a problem. Both my ItemTemplate and SelectedItemTemplate have a LinkButton whose Text property is Eval("Description"), but when I implement this solution, the LinkButton has no text. – zk812 Oct 07 '10 at 12:23

2 Answers2

3

My first answer was not so clever, mixed up listbox and listview, so i'll try again:

ListView1.DataSource = New String() {"i1", "i2", "i3", "i4", "i5"}
ListView1.SelectedIndex = 3
ListView1.DataBind()

if i put the second line last it does not work, Databind has to be called after setting the selectedindex, but you can also call Databind a second time, after setting the SelectedIndex

Willem
  • 5,364
  • 2
  • 23
  • 44
0

Something like this untested from memory

sId = Request.QueryString("id")
if NOT( string.NotisnullorEmpty(sId)) then
  Listbox.SelectedValue = sId
end if
Roadie57
  • 324
  • 1
  • 6
  • This is a ListView, not ListBox. It has no SelectedValue property. – zk812 Oct 06 '10 at 20:34
  • @zk812 - ListView does have a SelectedValue property, but that property only defines the get, not the set (so you can't change it for the ListView like you can for the ListBox). – dagilleland Jun 12 '12 at 20:01