Dropdown List Value

J

John

I have two dropdown lists that I have bound to a datatable and set the
DataTextField and DataValueField for. Both lists show the values I
expect from the database. However, when I need to access the
valueField for a dropdown list I always get the first value. Here is
my code I do on load to bind the dropdowns:

Try
'load the Division Dropdown
dtDiv = objDAL.ExecuteDataTable("GetDivision")
With ddDivision
.DataSource = dtDiv
.DataTextField = "DivisionName"
.DataValueField = "DivisionID"
.DataBind()
End With
If dtDiv.Rows.Count = 1 Then 'load the dropdown of sites
automatically otherwise wait for the selection
objParam.CreateParameter("DivID",
dtDiv.Rows(0).Item(0).ToString, DbType.Int32,
ParameterDirection.Input)
dtSite = objDAL.ExecuteDataTable("GetPlant", objParam)
With ddPlant
.DataSource = dtSite
.DataTextField = "PlantName"
.DataValueField = "PlantID"
.DataBind()
End With
End If

Again, this loads just fine. I have a button on the form that does a
redirect based on the values selected:

Response.Redirect("Default.aspx?DivID=" & ddDivision.SelectedValue &
"&PlantID=" & ddPlant.SelectedValue)

However the ddPlant.SelectedValue is always 1 (the first value in the
list) regardless of which value I select in the dropdown. In the
SelectedIndexChanged field of the dropdown I tried to set a session
variable but this event never fires. Anyone have any idea how to get
the selected values of dropdown lists?

Thanks.

John
 
J

John

It looks like you are rebinding your data on each postback so items are
cleared and the selected value is changed. You likely should use a "If Not
IsPostback" test to bind only if this is not a postback...

"John" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...








- Show quoted text -

That did it. Thanks.

John
 
B

bruce barker

this requires viewstate be turned on. a better approach is to disable
viewstate and databind in oninit.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top