Dropdownlist setting index to 0

T

tshad

Is there a way to stop the system from setting the SelectedIndex of a
dropdownlist to 0?

I have it set at -1 in my code and if I push a button (that does nothing in
my test), during PostBack it will be set to 0.

I know that when I initially bind it from a table, it will set the
SelectedIndex to 0. I then have to procedurally set it to a -1. But then
if I push a button that does nothing, it will reset it list to 0, if it
isn't set to something else. If it were set to a 2 or a 3 it will still
show it set to a 2 or 3. If it is set to nothing (-1), it will set it to 0.

Is there a way to stop this behavior?

Thanks,

Tom
 
C

Craig Deelsnyder

Is there a way to stop the system from setting the SelectedIndex of a
dropdownlist to 0?

I have it set at -1 in my code and if I push a button (that does nothing
in
my test), during PostBack it will be set to 0.

I know that when I initially bind it from a table, it will set the
SelectedIndex to 0. I then have to procedurally set it to a -1. But
then
if I push a button that does nothing, it will reset it list to 0, if it
isn't set to something else. If it were set to a 2 or a 3 it will still
show it set to a 2 or 3. If it is set to nothing (-1), it will set it
to 0.

Is there a way to stop this behavior?

Thanks,

Tom

No, because in HTML a dropdown (SELECT) automatically selects its first
item if no item was selected by default. One workaround is to add a
default or empty item at the top of the list and check against that being
selected (meaning nothing selected).
 
T

tshad

Craig Deelsnyder said:
No, because in HTML a dropdown (SELECT) automatically selects its first
item if no item was selected by default. One workaround is to add a
default or empty item at the top of the list and check against that being
selected (meaning nothing selected).

But if you set the SelectionMode to Multiple, you don't have this problem.
When I look at view source, I see an attribute "multiple" there.

Is there an attribute that says 0 or 1?

Thanks,

Tom
 
T

tshad

Craig Deelsnyder said:
No, because in HTML a dropdown (SELECT) automatically selects its first
item if no item was selected by default. One workaround is to add a
default or empty item at the top of the list and check against that being
selected (meaning nothing selected).
Also, if I set it to a -1, the html shows nothing being selected. But the
next time I post back, it does.

I assume that it is asp.net that is building the HTML to set the index. But
for some reason it has decided to set the first item as selected in some
instances.

Tom
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top