asp.net dropdown selected value

G

Guest

I must be missing something obvious. I have a drop down list in asp.net and it populates the states from a database. When I view the source of the page I get the standard option tags with the associated values. It all looks good. When I submit the form and some of the required fields aren't filled in the dropdown always reset to the first item in the list. First, how do I get it to stick. I've tried setting enableviewstate to true, but this does not seem to help. Second, and probably related, is that once the form is valid, it always passes the first item in the list regardless of which item is selected. I'm using the code below to set the value.

cboStateProvince.SelectedValue

And I'm populating the dropdown with the following code.

oda.SelectCommand.CommandType = CommandType.StoredProcedure
oda.Fill(ods)
cboStateProvince.DataSource = ods
cboStateProvince.DataValueField = "StateProvinceDisplayID"
cboStateProvince.DataTextField = "StateProvinceCombined"
cboStateProvince.DataBind()

Any suggestions?

Thanks,

Todd Meister
 
G

GSK

Are you populating the dropdown on PostBack? Try putting an

if (!Page.IsPostBack) {
....
}

around your code that initially sets the dropdown items.

- gsk.

tmeister said:
I must be missing something obvious. I have a drop down list in asp.net
and it populates the states from a database. When I view the source of the
page I get the standard option tags with the associated values. It all
looks good. When I submit the form and some of the required fields aren't
filled in the dropdown always reset to the first item in the list. First,
how do I get it to stick. I've tried setting enableviewstate to true, but
this does not seem to help. Second, and probably related, is that once the
form is valid, it always passes the first item in the list regardless of
which item is selected. I'm using the code below to set the value.
 
G

Guest

That worked. Thanks a lot.

GSK said:
Are you populating the dropdown on PostBack? Try putting an

if (!Page.IsPostBack) {
....
}

around your code that initially sets the dropdown items.

- gsk.


and it populates the states from a database. When I view the source of the
page I get the standard option tags with the associated values. It all
looks good. When I submit the form and some of the required fields aren't
filled in the dropdown always reset to the first item in the list. First,
how do I get it to stick. I've tried setting enableviewstate to true, but
this does not seem to help. Second, and probably related, is that once the
form is valid, it always passes the first item in the list regardless of
which item is selected. I'm using the code below to set the value.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top