asp.net dropdown selected value

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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.
 
Back
Top