Dropdownlist setting index to 0

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