Problem with changing the index of the SelectedValue of a DropDownList

T

Thomas Kern

Hi everybody!

I have a dropdownlist on a aspx page. The dropdownlist is filled in the
page_load event like this:

If Not Page.IsPostBack Then
BindData()
End If

The Bind Data method goes to the database and reads the values for the
dropdownlist. In addition it selects the appropriate value:

ddlKind.SelectedValue = row.FK_KIND 'row.FK_KIND is from another table
ddl.DataBind()

This works fine!

Now I implemented the possibility for users to change information and save
it. This is where my problems occur after the user hits the "Save" button my
save event does the changes to the data. In the end i call the BindData()
method again to repopulate everything.
But this time the ddlKind does not get selected appropriate. It always
selects the first entry! Although in the debugger I can see that
ddlKind.SelectedValue is set to the correct value.

Any ideas what I might do wrong?

Thanks for help.
rgds,
tom
 
G

Guest

I suspect your problem is that you are rebinding your dropdoanlist when you
don't need to.

Remember, the VIEWSTATE? This is going to store the current values in your
ddl (and the selected item) between calls to the page.

BUT if you rebind - you'll zap the viewstate and it will be repopulated with
the values from the db with nothing selected.

So: I reckon you need to ONLY bind your ddl on the very first Page_Load NOT
when you've saved the data too...

HTH

(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top