Avatar of GENTP
GENTP

asked on 

ListBox With Multiple Selection Mode

I have a ListBox in an ASP.NET 1.1 app. SelectionMode is set to Multiple. Nothing is currently happening with regards to the listbox on form submit, so there is nothing happening in my button's click event that is causing the error.

Immediately upon selecting 2 (or more) items in the list, and clicking submit, I am hit with an error telling me a dropdownlist cannot have multiple items select. I understand this is an issue revolving around listbox and dropdownlist using the same base class, but have no idea how to fix it.

Any suggestions? (Full error follows)


A DropDownList cannot have multiple items selected.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A DropDownList cannot have multiple items selected.




Thanks
Editors IDEs.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Havagan
Avatar of G0ggy
G0ggy
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of GENTP
GENTP

ASKER

1) I am not using a dropdownlist.
2) I am not using the FindByValue method.

Why did you post that link?
Avatar of Havagan
Havagan

Please post the code block which is throwing the exception.
Avatar of G0ggy
G0ggy
Flag of United Kingdom of Great Britain and Northern Ireland image

Enumerating the items and methods is the same.
Avatar of GENTP
GENTP

ASKER

There doesn't appear to be a code block throwing the error. I have put the click event to:
this.btnSubmit.Click += new System.EventHandler(this.btnTest);

And setup btnTest as an empty function, which still craps out.

private void btnTest(object sender, System.EventArgs e)
{
}

The control in question is declared as:
<asp:ListBox id="lstDepts" runat="server" Rows="6" SelectionMode="Multiple"></asp:ListBox>

It's quite the confusing error, considering the purpose of multiple select is to select multiple items...
ASKER CERTIFIED SOLUTION
Avatar of Havagan
Havagan

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of GENTP
GENTP

ASKER

I have found that the problem occurs during init.

What I have been doing is populating a drop down from the DB, then copying its itemlist to the other controls, to save DB hits. So I had the following:

foreach(ListItem tpItem in dropDept.Items)
{
      dropGraphDept.Items.Add(tpItem);
      lstDepts.Items.Add(tpItem);
}

It would appear that the listItem used by a listbox and a dropdownlist are slightly different. I modified to declare a new listitem, as follows:
lstDepts.Items.Add(new ListItem(tpItem.Text.ToString(), tpItem.Value.ToString()));
and it appears to work. Which I believe makes this a referencing problem.

Thanks for the help!
Avatar of Havagan
Havagan

The DropDownList and ListBox both inherit from ListControl. Their collection of items is a ListItemCollection and both use the same ListItem class. You probably just needed to set the .Selected property to false before adding it to the drop down list.

foreach(ListItem tpItem in dropDept.Items)
{
      lstDepts.Items.Add(tpItem);
      tpItem.Selected = false;
      dropGraphDept.Items.Add(tpItem);
}

To save trips to the database you probably would have been better off retrieving the data into a datatable and caching it. Then just binding the datatable to the various list controls on the initial page load and letting viewstate handle loading the controls again on postbacks.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo