473,242 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

DropDownList with multiple selected items error

I'm having the problem with this drop down list on postback. For some
reason both the ListItems get selected when I change the selected item.
Using the code below I'm building the drop down list in the overriden
CreateChildControls method and setting the selected item. Then when I
change the item in the drop down list the list is rebuilt from
viewstate, but the initial item is still selected causing the error
below.

I also have a number of other drop down lists in this custom control
that work just fine, so its a bit confusing to me why only this one
doesn't work.

Can anyone see what might be causing this?

ERROR:

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

CODE:

CreateChildControls()
{
this.rearTravelList = (DropDownList)Page.FindControl("rearTravelList");

this.rearTravelList.AutoPostBack = true;
this.rearTravelList.SelectedIndexChanged += new EventHandler(
this.SelectedRearTravelChanged );
if ( !Page.IsPostBack )
this.BuildRearTravelList();
}

private void BuildRearTravelList()
{
ListItem xCountryItem = new ListItem();
ListItem freeRideItem = new ListItem();

xCountryItem.Text = "X Country 2.25-4.49in";
xCountryItem.Value = RidingStyle.XC.ToString();

freeRideItem.Text = "Free Ride 4.5-6in";
freeRideItem.Value = RidingStyle.FR.ToString();

this.rearTravelList.Items.Add( xCountryItem );
this.rearTravelList.Items.Add( freeRideItem );

this.SetSelectedItem( this.rearTravelList, this.RidingStyle.ToString()
);
}

private void SetSelectedItem(ListControl list, string textValue)
{
list.ClearSelection();
ListItem item = list.Items.FindByValue( textValue );
if ( item != null )
item.Selected = true;
}
Nov 18 '05 #1
1 3441
I have an update to this problem. In the CreateChildControls method I
add the line

this.rearTravelList.ClearSelection();

so the code looks like this.

CreateChildControls()
{
this.rearTravelList =
(DropDownList)Page.FindControl("rearTravelList");

this.rearTravelList.ClearSelection();
this.rearTravelList.AutoPostBack = true;
this.rearTravelList.SelectedIndexChanged += new EventHandler(
this.SelectedRearTravelChanged );
if ( !Page.IsPostBack )
this.BuildRearTravelList();
}

After adding the new line of code I can now select the new item and it
works correctly in selecting the item in the dropdownlist and doesn't
give me the multiple items selected error, but in the process of doing
this the SelectedIndexChanged event is not being fired.

Can anyone explain this?

Aaron

Aaron Prohaska wrote:

I'm having the problem with this drop down list on postback. For some
reason both the ListItems get selected when I change the selected item.
Using the code below I'm building the drop down list in the overriden
CreateChildControls method and setting the selected item. Then when I
change the item in the drop down list the list is rebuilt from
viewstate, but the initial item is still selected causing the error
below.

I also have a number of other drop down lists in this custom control
that work just fine, so its a bit confusing to me why only this one
doesn't work.

Can anyone see what might be causing this?

ERROR:

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

CODE:

CreateChildControls()
{
this.rearTravelList = (DropDownList)Page.FindControl("rearTravelList");

this.rearTravelList.AutoPostBack = true;
this.rearTravelList.SelectedIndexChanged += new EventHandler(
this.SelectedRearTravelChanged );
if ( !Page.IsPostBack )
this.BuildRearTravelList();
}

private void BuildRearTravelList()
{
ListItem xCountryItem = new ListItem();
ListItem freeRideItem = new ListItem();

xCountryItem.Text = "X Country 2.25-4.49in";
xCountryItem.Value = RidingStyle.XC.ToString();

freeRideItem.Text = "Free Ride 4.5-6in";
freeRideItem.Value = RidingStyle.FR.ToString();

this.rearTravelList.Items.Add( xCountryItem );
this.rearTravelList.Items.Add( freeRideItem );

this.SetSelectedItem( this.rearTravelList, this.RidingStyle.ToString()
);
}

private void SetSelectedItem(ListControl list, string textValue)
{
list.ClearSelection();
ListItem item = list.Items.FindByValue( textValue );
if ( item != null )
item.Selected = true;
}

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
1
by: DC Gringo | last post by:
I'm having such a problem with this DropDownList in a user control that is posting back and throwing an error: System.Web.HttpException: A DropDownList cannot have multiple items selected ...
2
by: Tom Bray | last post by:
Ok I am baffled I can not figure out this problem. I am getting the following error: Portal Error - A DropDownList cannot have multiple items selected. Error information Name Value Message...
6
by: tfs | last post by:
I have a dropdownlist that I was setting to an index (which happened to be the same as the value). I changed this to a character as that is what I want to have in my database. <asp:dropdownlist...
0
by: dhnriverside | last post by:
Hi peeps OK, I'm getting a weird error trying to select a DDL item based on what's stored in the database. I have 4 DDLs on my aspx page... <asp:dropdownlist id="ddlStartTimeHour"...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
2
by: =?Utf-8?B?S3Jpc2huYQ==?= | last post by:
Hi, I am devloping one web application using .net framework 2.0.One page has 7 dropdown list control.When i update the values first bind the values to the drop down llist then selected text...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.