problem with dropdownlist selected item

Discussion in 'ASP .Net Building Controls' started by Aaron Prohaska, Dec 3, 2003.

  1. 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;
    }
     
    Aaron Prohaska, Dec 3, 2003
    #1
    1. Advertising

  2. 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;
    > }
     
    Aaron Prohaska, Dec 3, 2003
    #2
    1. Advertising

  3. Aaron Prohaska

    NoOne Guest

    You have to note when CreateChildControls is called in the page lifecycle
    and be careful what you do there, especially if your control is put onto a
    web form as opposed to added in the codebehind logic. If its called early in
    the page construction then IsPostBack might not be set yet, which would
    explain why you were getting duplicated items. Also this would also explain
    why your selected changed event is not getting fired.



    "Aaron Prohaska" <> wrote in message
    news:...
    > 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;
    > > }
     
    NoOne, Dec 4, 2003
    #3
  4. Ok, I understand that, but I don't know what to do about it. Would you
    please take a look at another post that I just made to this group with
    the subject "Composite control building architecture problem". It has a
    much more complete example of my problem with all the code that goes
    along with it.

    thanks for the reply,

    Aaron

    NoOne wrote:
    >
    > You have to note when CreateChildControls is called in the page lifecycle
    > and be careful what you do there, especially if your control is put onto a
    > web form as opposed to added in the codebehind logic. If its called early in
    > the page construction then IsPostBack might not be set yet, which would
    > explain why you were getting duplicated items. Also this would also explain
    > why your selected changed event is not getting fired.
    >
    > "Aaron Prohaska" <> wrote in message
    > news:...
    > > 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;
    > > > }
     
    Aaron Prohaska, Dec 4, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    971
  2. Nathan Sokalski
    Replies:
    0
    Views:
    653
    Nathan Sokalski
    Oct 5, 2008
  3. mldardy
    Replies:
    0
    Views:
    1,002
    mldardy
    Sep 28, 2010
  4. EJD

    dropdownlist selected item problem

    EJD, May 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    151
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    318
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page