Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > problem with dropdownlist selected item

Reply
Thread Tools

problem with dropdownlist selected item

 
 
Aaron Prohaska
Guest
Posts: n/a
 
      12-03-2003
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;
}
 
Reply With Quote
 
 
 
 
Aaron Prohaska
Guest
Posts: n/a
 
      12-03-2003
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;
> }

 
Reply With Quote
 
 
 
 
NoOne
Guest
Posts: n/a
 
      12-04-2003
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;
> > }



 
Reply With Quote
 
Aaron Prohaska
Guest
Posts: n/a
 
      12-04-2003
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;
> > > }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net Web Controls 0 10-05-2008 10:29 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net 0 10-05-2008 10:29 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
dropdownlist selected item problem EJD ASP .Net Web Controls 0 05-11-2005 11:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57