zk812
zk812

Reputation: 81

How can I set a ListView's selected item based on it's ID in a query string on page load?

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.

Upvotes: 2

Views: 1976

Answers (2)

Willem
Willem

Reputation: 5404

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

Upvotes: 3

Roadie57
Roadie57

Reputation: 324

Something like this untested from memory

sId = Request.QueryString("id")
if NOT( string.NotisnullorEmpty(sId)) then
  Listbox.SelectedValue = sId
end if

Upvotes: 0

Related Questions