Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Why can't I set the listbox selectedIndex to -1?

Reply
Thread Tools

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

 
 
amtamayo@gmail.com
Guest
Posts: n/a
 
      04-26-2007
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.

 
Reply With Quote
 
 
 
 
Ray Costanzo
Guest
Posts: n/a
 
      04-26-2007
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:

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

Note the AppendDataBoundItems attribute.

Ray at work



<> wrote in message
news: ups.com...
>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.
>


 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      04-26-2007
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.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
ListBox not returning correct SelectedIndex =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 1 02-04-2005 03:49 PM
selectedindex of multiple listbox selection in a datagrid =?Utf-8?B?TGll?= ASP .Net 5 12-14-2004 02:01 AM
ListBox SelectedIndex is always -1 S. Justin Gengo ASP .Net 2 02-17-2004 06:10 PM
Asp:ListBox and selectedindex John Giblin ASP .Net 0 11-26-2003 08:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57