Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Preselect Dropdownlist

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-28 : 17:19:38
I have a dropdownlist with the values 1-10. What I want is when the page loads, for the dropdownlist to show the value in the database. If the value is null, it'll go to 1.

Right now, if I select from the db, whatever value is there is the first in my collection. So, if the value is 8 in the db, then my dropdownlist looks like 8,2,3,4,5,6,7,8,9,10...and there's no 1 to select.

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-28 : 17:44:06
Looks like this may do the trick, but it's not for dynamic data!

I'm using a test db here in the code below, but I can simply do a Case Statement in my live db to figure it out. This is a static list, so I don't have to worry about more than 10 possible values. I'd like to know how to do this with a large dynamic table.

Before, I was using:

SelectedItem.Value = Find.ToString()

....which was overwriting the first Member of the Collection.

Here's what worked:

Con.Open()
Dim str As String
str = "Select Day FROM DOW WHERE ID = 4"
Dim cmd As New OleDb.OleDbCommand(str, Con)
Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
dr.Read()
Dim Find As String = dr("Day")
If Find = "Thursday" Then 'Thursday is ID = 4
DropDownList1.SelectedIndex = 3 'Thursday
Else
DropDownList1.SelectedIndex = 0 'Monday
End If
dr.Close
dr = nothing
Con.Close()

Any other suggestions are welcome!

Thanks!
Go to Top of Page
   

- Advertisement -