We help IT Professionals succeed at work.

ASP.NET: Problem with listbox that has Multiple selection

469 Views
Last Modified: 2013-11-07
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.
Comment
Watch Question

erikTsomikSystem Architect, CF programmer

Commented:
can you post the code . You probably have first marked as selected by default

Author

Commented:
I didn't set the first element as selected by default since I feed the list dynamically.

Commented:
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.

Commented:
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.

Author

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;
		    break;
		}
	    }
	}
	if (anySelected) {
	}
	else {
	    buttonDisplay = 'none'
	}
 
	document.getElementById('<%=btnQuit.ClientID%>').style.display = buttonDisplay;
    }
}

Open in new window

Author

Commented:
I connect the function this way:
                            <asp:ListBox ID="lbTheList" runat="server" SelectionMode="Multiple"
                                onchange="ShowHideButton();" />
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.

Commented:
Sometimes you just need fresh eyes :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.