Solved

ASP.NET: Problem with listbox that has Multiple selection

Posted on 2009-07-08
10
444 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now