Solved

ASP.NET: Problem with listbox that has Multiple selection

Posted on 2009-07-08
10
448 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

696 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