468,103 Members | 1,252 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

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 3098
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by tfs | last post: by
reply views Thread by dhnriverside | last post: by
10 posts views Thread by ads | last post: by
4 posts views Thread by rn5a | last post: by
2 posts views Thread by =?Utf-8?B?S3Jpc2huYQ==?= | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.