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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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 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)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.