Dropdownlist setting index to 0

Discussion in 'ASP .Net' started by tshad, Apr 19, 2005.

  1. tshad

    tshad Guest

    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
     
    tshad, Apr 19, 2005
    #1
    1. Advertising

  2. On Tue, 19 Apr 2005 12:53:19 -0500, tshad <>
    wrote:

    > 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
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Apr 19, 2005
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Craig Deelsnyder" <cdeelsny@no_spam_4_meyahoo.com> wrote in message
    news:eek:...
    > On Tue, 19 Apr 2005 12:53:19 -0500, tshad <>
    > wrote:
    >
    >> 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).
    >


    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
    > Microsoft MVP - ASP/ASP.NET
     
    tshad, Apr 19, 2005
    #3
  4. tshad

    tshad Guest

    "Craig Deelsnyder" <cdeelsny@no_spam_4_meyahoo.com> wrote in message
    news:eek:...
    > On Tue, 19 Apr 2005 12:53:19 -0500, tshad <>
    > wrote:
    >
    >> 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).
    >

    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
    > --
    > Craig Deelsnyder
    > Microsoft MVP - ASP/ASP.NET
     
    tshad, Apr 19, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    650
    Bryce
    Mar 17, 2005
  2. wrecker
    Replies:
    1
    Views:
    188
    Wardeaux
    Feb 13, 2004
  3. Shawn W_
    Replies:
    5
    Views:
    312
    Aldric Giacomoni
    Sep 16, 2009
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    337
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    207
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page