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

M

mc

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
 
J

Jared

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
 
W

Winista

I have a feeling that you are resetting the selected index value of your DDL
somewhere in Page_Load or some handler.
 
M

mc

Jared said:
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:

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

e.g.

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


Mike
 
M

mc

Winista said:
I have a feeling that you are resetting the selected index value of your DDL
somewhere in Page_Load or some handler.

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
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top