views:

43

answers:

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.

A: 

Something like this untested from memory

sId = Request.QueryString("id")
if NOT( string.NotisnullorEmpty(sId)) then
  Listbox.SelectedValue = sId
end if
Roadie57
This is a ListView, not ListBox. It has no SelectedValue property.
zk812
+2  A: 

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
My ListView's items collection doesn't have a FindByValue routine...?
zk812
you are right, I mixed up listbox and listview, sorry for that...
Willem
Awesome!! Can't believe it was that easy...
zk812