Why can't I set the listbox selectedIndex to -1?

Discussion in 'ASP .Net' started by amtamayo@gmail.com, Apr 26, 2007.

  1. Guest

    I have a simple webform that has a listbox that I bind to a dataview
    at designtime. This provides an optional selection for the user so I
    wanted to have no default value selected. So on the Page_Load event,
    I set the selectedIndex to -1. However, when I run the page, the
    default value is still 0, which forces the first item to be
    automatically selected. I tried a bunch of things. I tried setting
    it to 0, then -1. I tried to call ClearSelection() then set it to -1,
    but no avail. This is a listbox, for crying out loud! This ought to
    be simple, but is not.

    Need help.
     
    , Apr 26, 2007
    #1
    1. Advertising

  2. Ray Costanzo Guest

    This is just how most (all maybe?) browsers work. If you create a static
    html file and put a <select> in it with some options, the first one is
    selected by default in every browser I've used. According to W3C, this is
    up to the browser to determine what the right thing to do is.
    http://www.w3.org/TR/html4/interact/forms.html#h-17.6 "If no OPTION element
    has the selected attribute set, user agent behavior for choosing which
    option is initially selected is undefined."

    Perhaps this will be a satisfactory option:

    <asp:DropDownList ID="ddl" runat="server" DataValueField="ID"
    DataTextField="Display" AppendDataBoundItems="true">
    <asp:ListItem Value="">Select an option</asp:ListItem>
    </asp:DropDownList>

    Note the AppendDataBoundItems attribute.

    Ray at work



    <> wrote in message
    news:...
    >I have a simple webform that has a listbox that I bind to a dataview
    > at designtime. This provides an optional selection for the user so I
    > wanted to have no default value selected. So on the Page_Load event,
    > I set the selectedIndex to -1. However, when I run the page, the
    > default value is still 0, which forces the first item to be
    > automatically selected. I tried a bunch of things. I tried setting
    > it to 0, then -1. I tried to call ClearSelection() then set it to -1,
    > but no avail. This is a listbox, for crying out loud! This ought to
    > be simple, but is not.
    >
    > Need help.
    >
     
    Ray Costanzo, Apr 26, 2007
    #2
    1. Advertising

  3. bruce barker Guest

    this can only be done in javascript. in html if you render a select with
    no option set as selected, it defaults to the first. in javascript you
    can set the selectedIndex to -1 which will unselect.

    you can subclass the listbox, and have it generate the required javascript.

    -- bruce (sqlwork.com)

    wrote:
    > I have a simple webform that has a listbox that I bind to a dataview
    > at designtime. This provides an optional selection for the user so I
    > wanted to have no default value selected. So on the Page_Load event,
    > I set the selectedIndex to -1. However, when I run the page, the
    > default value is still 0, which forces the first item to be
    > automatically selected. I tried a bunch of things. I tried setting
    > it to 0, then -1. I tried to call ClearSelection() then set it to -1,
    > but no avail. This is a listbox, for crying out loud! This ought to
    > be simple, but is not.
    >
    > Need help.
    >
     
    bruce barker, Apr 26, 2007
    #3
    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. John Giblin

    Asp:ListBox and selectedindex

    John Giblin, Nov 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,123
    John Giblin
    Nov 26, 2003
  2. S. Justin Gengo

    ListBox SelectedIndex is always -1

    S. Justin Gengo, Feb 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,799
  3. =?Utf-8?B?TGll?=
    Replies:
    5
    Views:
    693
    =?Utf-8?B?TGll?=
    Dec 14, 2004
  4. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    ListBox not returning correct SelectedIndex

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,334
    Karl Seguin
    Feb 4, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,231
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page