Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving selected value from drop down list 1

Status
Not open for further replies.
Nov 29, 2001
72
US
Guys,
I am muddling my way thru .NET so this may seem like a very basic question.

I have a DDL populated by a SQL table and I set the selected value based on a passed parameter. This seems to work fine and actually displays the correct value...

Private Sub Page_Load(blah, blah, blah)

'Schedule Type drop down box
SchTypeDDL.SelectedItem.Text = schedID
SchTypeDDL.SelectedItem.Value = schedID
cmd = New SqlCommand( _
"SELECT * FROM PhyCodes " _
& "WHERE codetype = " & "'" & "ST" & "' " _
& "ORDER BY code", cnn)
cnn.Open()
rdr = cmd.ExecuteReader CommandBehavior.CloseConnection)
While rdr.Read
SchTypeDDL.Items.Add(New ListItem(rdr("code")))
End While
rdr.Close()
end sub

Once I change the drop down box selection and click a submit button, I simply want to retrieve it for a table update.

Private Sub Submit_Click(blah, blah, blah)
dim schedID as string

schedID = SchTypeDDL.SelectedValue
end sub

This always returns the original selected value and not the new selected value.

As I said I'm new to .NET. Can anyone spot the bonehead move I am making. THis is so simple in ASP classic that I can't believe I cannot get this to work.

Thanks in advance,
Dave

 
Well it looks like in your page load you're repopulating the textbox on each postback. Put in a check first to see if the page is posting back ... if not (its the original page load) then populate your dropdown. You should then be able to collect the value using:

SchTypeDDL.SelectedValue
or
SchTypeDDl.SelectedItem.Value

If you prefer to repopulate the dropdown box on each postback (which I would not recommend when having to pull it each time from a database) then you can keep the code the way you have it and try to retrieve the selected value like this instead:

Request.Form ["SchTypeDDL"].Value

I'm not sure what the syntax would be for VB.NET which is what it looks like you're using, but I hope that this will help.

- VB Rookie
 
Thanks VB Rookie.

Your solution makes perfect sense. Thanks for your help.

.NET Rookie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top