469,638 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,638 developers. It's quick & easy.

Dropdown List Value

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
Oct 13 '08 #1
3 2573
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" <ri**********@hotmail.coma écrit dans le message de groupe de
discussion :
25**********************************...oglegroups.com...
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

Oct 13 '08 #2
On Oct 13, 9:55*am, "Patrice" <http://www.chez.com/scribe/wrote:
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" <riley_wri...@hotmail.coma écrit dans le message de groupe de
discussion :
25cadd16-e2bf-4405-b97d-e17c8b38b...@m74g2000hsh.googlegroups.com...
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- Hide quoted text -

- Show quoted text -
That did it. Thanks.

John
Oct 13 '08 #3
this requires viewstate be turned on. a better approach is to disable
viewstate and databind in oninit.

-- bruce (sqlwork.com)
"Patrice" wrote:
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" <ri**********@hotmail.coma crit dans le message de groupe de
discussion :
25**********************************...oglegroups.com...
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

Oct 13 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike Collins | last post: by
5 posts views Thread by jung_h_park | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.