473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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 '07 #1
2 1756
This is just how most (all maybe?) browsers work. If you create a static
html file and put a <selectin 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

<am******@gmail.comwrote in message
news:11*********************@u32g2000prd.googlegro 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.
Apr 26 '07 #2
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)

am******@gmail.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.
Apr 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Russell Campbell | last post by:
Posting again, since my first attempt never appeared: In the onClick method of a listbox, I am attempting to retrieve the selectedIndex property. The listbox is set up to allow multiple...
0
by: L.Peter | last post by:
Dear Group, I am doing Drag and Drop function and see this problem if I select 3 from listBox1 and drag to listBox2, the value in listBox1 is not updated, even I move (or use up and down keyboard)...
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
3
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default...
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
0
by: deepak | last post by:
Hi all, i m transefering values from one listbox to another listbox using javascript(both are html controls), the first listbox is populating with database at page_load time and the second listbox...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
1
by: fiaolle | last post by:
Hi I Can't get the MSDN's example for Listbox to work.The example is below. I get an error in Listbox's sub ListBox1_SelectedValueChanged at row textBox1.Text = ListBox1.SelectedValue and it...
3
by: crsqn91l | last post by:
frnds i m not getting first item of listbox details but i m getting otherthan first item listbox details when i click..on item in listbox. for loading values i m using below code.. public void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.