Solved

ASP.NET: Problem with listbox that has Multiple selection

Posted on 2009-07-08
10
445 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.
0
Comment
Question by:karakav
10 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24806319
can you post the code . You probably have first marked as selected by default
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24806345
0
 
LVL 4

Author Comment

by:karakav
ID: 24806498
I didn't set the first element as selected by default since I feed the list dynamically.
0
 
LVL 3

Expert Comment

by:Kelevra
ID: 24806983
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.
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24810573
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.

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Author Comment

by:karakav
ID: 24813355
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

0
 
LVL 4

Author Comment

by:karakav
ID: 24813448
I connect the function this way:
                            <asp:ListBox ID="lbTheList" runat="server" SelectionMode="Multiple"
                                onchange="ShowHideButton();" />
0
 
LVL 20

Accepted Solution

by:
ddayx10 earned 500 total points
ID: 24819352
For me at least, changing one line of your JS function fixes the problem:

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

To:
if (listBox.options[i].selected == true)
0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31601236
Thanks a lot. You can't imagine how much I apreciate your help. I revised my code so many times without finding that bug.
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24828834
Sometimes you just need fresh eyes :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now