• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

ASP.NET: Problem with listbox that has Multiple selection

hi there,

I have a listbox that I populate dynamically. The selection mode is set to Multiple. The problem is that when I select an item from the listbox, the first element is selected automatically as well. I would like to know how I can get rid of that problem.
1 Solution
erikTsomikSystem Architect, CF programmer Commented:
can you post the code . You probably have first marked as selected by default
karakavAuthor Commented:
I didn't set the first element as selected by default since I feed the list dynamically.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

You didn't set an element as selected by default on purpose?

Because feeding the list dynamically, would not be a raison for not doing it!

just a thought.
Weird, that is certainly not the normal behavior of this control whether you set a listitem to be selected or not...consider that you have to hold the CTRL down to select multiples...

I agree with erikTsomik that we need to see the code where the listbox is populated and any events it raises...as well as the context it is in on the .aspx page.

Only scenarios I can see are:
1) Event is raised (onselectedindexchanged/ontextchanged) and in this event the items are being manipulated incorrectly.
2) Some javascript activities are messing with things.
3) Another control is interfering in some unexpected way (updatepanel).

but that doesn't mean it's not something else...just thinkin outloud.

karakavAuthor Commented:
Yeah you are right. Actually it is a function that is messing up. the problem is that I need that function to show and hidden the button used to remove items from the list.
function ShowHideButton() {
    var listBox = document.getElementById('<%=lbTheList.ClientID%>');
    var anySelected = false;
    var buttonDisplay = '';
    var anySelectedIndex = 0;
    if (listBox) {
	if (listBox.options.length > 0) {
	    for (var i = 0; i < listBox.options.length; i++) {
		if (listBox.options[i].selected = true) {
		    anySelected = true;
		    tmpIdProvincia = listBox.options[i].value;
	if (anySelected) {
	else {
	    buttonDisplay = 'none'
	document.getElementById('<%=btnQuit.ClientID%>').style.display = buttonDisplay;

Open in new window

karakavAuthor Commented:
I connect the function this way:
                            <asp:ListBox ID="lbTheList" runat="server" SelectionMode="Multiple"
                                onchange="ShowHideButton();" />
For me at least, changing one line of your JS function fixes the problem:

if (listBox.options[i].selected = true)

if (listBox.options[i].selected == true)
karakavAuthor Commented:
Thanks a lot. You can't imagine how much I apreciate your help. I revised my code so many times without finding that bug.
Sometimes you just need fresh eyes :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now