Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Copying items from a Drop Down List to a List Box?

Reply
Thread Tools

Copying items from a Drop Down List to a List Box?

 
 
mc
Guest
Posts: n/a
 
      06-23-2006
I've got three controls on a page

a Drop Down List, a List Box and a Button

I've added 3 items to the Drop Down List

I have added some code in the Button onClick Event Handler, as below
ListBox1.Items.Addd(DropDownList1.SelecteItem);
ListBox1.ClearSelection();

If I select the 2nd item in the list and click the button, item 2 gets
added to the ListBox but the Selection of the Drop Down List is Forgotten!

Why??

Regards


Mike
 
Reply With Quote
 
 
 
 
Jared
Guest
Posts: n/a
 
      06-23-2006
Hi

I suspect you are re-populating your dropdown list on every post back.

If you wrap the code in

if(!IsPostBack)
{


}

It should resolve your issue.

Jared

mc wrote:

> I've got three controls on a page
>
> a Drop Down List, a List Box and a Button
>
> I've added 3 items to the Drop Down List
>
> I have added some code in the Button onClick Event Handler, as below
> ListBox1.Items.Addd(DropDownList1.SelecteItem);
> ListBox1.ClearSelection();
>
> If I select the 2nd item in the list and click the button, item 2 gets
> added to the ListBox but the Selection of the Drop Down List is Forgotten!
>
> Why??
>
> Regards
>
>
> Mike


 
Reply With Quote
 
 
 
 
Winista
Guest
Posts: n/a
 
      06-23-2006
I have a feeling that you are resetting the selected index value of your DDL
somewhere in Page_Load or some handler.


"mc" <(E-Mail Removed)> wrote in message
news:449be44f$(E-Mail Removed)...
> I've got three controls on a page
>
> a Drop Down List, a List Box and a Button
>
> I've added 3 items to the Drop Down List
>
> I have added some code in the Button onClick Event Handler, as below
> ListBox1.Items.Addd(DropDownList1.SelecteItem);
> ListBox1.ClearSelection();
>
> If I select the 2nd item in the list and click the button, item 2 gets
> added to the ListBox but the Selection of the Drop Down List is Forgotten!
>
> Why??
>
> Regards
>
>
> Mike



 
Reply With Quote
 
mc
Guest
Posts: n/a
 
      06-23-2006
Jared wrote:
> Hi
>
> I suspect you are re-populating your dropdown list on every post back.
>
> If you wrap the code in
>
> if(!IsPostBack)
> {
>
>
> }
>
> It should resolve your issue.
>
> Jared
>
> mc wrote:
>
>
>>I've got three controls on a page
>>
>>a Drop Down List, a List Box and a Button
>>
>>I've added 3 items to the Drop Down List
>>
>>I have added some code in the Button onClick Event Handler, as below
>> ListBox1.Items.Addd(DropDownList1.SelecteItem);
>> ListBox1.ClearSelection();
>>
>>If I select the 2nd item in the list and click the button, item 2 gets
>>added to the ListBox but the Selection of the Drop Down List is Forgotten!
>>
>>Why??
>>
>>Regards
>>
>>
>>Mike

>
>


Nope, I'm not doing any data binding, the Drop Down List Items are all
defined in the aspx file.

e.g.

<aspropDownList ID="blah" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</aspropDownList>


Mike
 
Reply With Quote
 
mc
Guest
Posts: n/a
 
      06-23-2006
Winista wrote:
> I have a feeling that you are resetting the selected index value of your DDL
> somewhere in Page_Load or some handler.
>
>
> "mc" <(E-Mail Removed)> wrote in message
> news:449be44f$(E-Mail Removed)...
>
>>I've got three controls on a page
>>
>>a Drop Down List, a List Box and a Button
>>
>>I've added 3 items to the Drop Down List
>>
>>I have added some code in the Button onClick Event Handler, as below
>>ListBox1.Items.Addd(DropDownList1.SelecteItem) ;
>>ListBox1.ClearSelection();
>>
>>If I select the 2nd item in the list and click the button, item 2 gets
>>added to the ListBox but the Selection of the Drop Down List is Forgotten!
>>
>>Why??
>>
>>Regards
>>
>>
>>Mike

>
>
>


I first found the problem on one of my "Real" pages, so to test it I've
used a new blank page. The only code in the entire page is the event
handler code!

Try it for yourself?

Regards


Mike Caddy
 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      06-26-2006
Hi Mike,

Thank you for your post.

Since you're directly adding the ListItem to the ListBox from DropDownList,
they're sharing the same ListItem. When you call ListBox's ClearSelection()
method, it set's the ListItem's Selected property to false, which also
clears the DropDownList's ListItem's Selected property.

You can use following two workarounds for this problem:

1) Still use the same ListItem, so you don't have to worry about the
ListItem's Value property:

if (ListBox1.SelectedItem != null)
{
ListBox1.SelectedItem.Selected = false;
}
ListBox1.Items.Add(DropDownList1.SelectedItem);

2) Copy the ListItem:

ListItem li = new ListItem(DropDownList1.SelectedItem.Text,
DropDownList1.SelectedItem.Value);
ListBox1.Items.Add(li);

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ((E-Mail Removed), remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

 
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
retrive preselected value in second drop down list from the first drop down list weiwei ASP .Net 0 01-05-2007 07:29 PM
How do I create a Drop Down List with Hyperlinks as List Items in ASP.NET Tony Tone ASP .Net Web Controls 1 04-29-2006 05:04 PM
How do I create a Drop Down List with Hyperlinks as List Items in ASP.NET Tony Tone ASP .Net 2 04-28-2006 07:33 PM
How do I create a Drop Down List with Hyperlinks as List Items in ASP.NET Tony Tone ASP .Net 0 04-28-2006 06:38 PM
Auto Drop down a Drop Down List xxbmichae1@supergambler.com Javascript 5 11-23-2005 01:35 AM



Advertisments