How do I move a dropdownlist to a selectedindex value?

J

jm

I know how to set the ddl.selectedindex = x. That is not the problem.

I have a dynamically populated DropDownList populated from a database.
I use the onitemdatabound to populate the list when the edit link is
clicked. What I want it to do is put the dropdownlist's selectedindex
at the value it was at when the user first entered data.

So, for example, the ddl has:

smith
jones
johnson

the user clicked jones when they first entered the data, but now, the
hit edit. I don't want the ddl to be put back at smith. I want the
list to start at jones (the list is the same; I just mean what is
displayed as the selectedindex).

I just cannot figure out where to set it.

I have my onitemdatabound event that populates it, but when I tried to
read the current selected index just before I populate it, it is too
late. I get -1; something has changed and I don't know where to get
the current selectedindex value that was there when edit was clicked.

Thank you for any help.
 
C

Craig Deelsnyder

jm said:
I have my onitemdatabound event that populates it, but when I tried to
read the current selected index just before I populate it, it is too
late. I get -1; something has changed and I don't know where to get
the current selectedindex value that was there when edit was clicked.

Thank you for any help.

You normally do it after binding (or populating) its values completely.
So after you ddlMyDropdown.DataBind() statement....

The reasons for this are what you're seeing. You don't want to set the
index until all the items are 'there', otherwise something could get
inserted at a certain position, etc. And I'm sure the event is doing
this to you (either cancelling the set or resetting it when the DataBind
is done)
 

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,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top