473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jun 23 '06 #1
5 2556
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


Jun 23 '06 #2
I have a feeling that you are resetting the selected index value of your DDL
somewhere in Page_Load or some handler.
"mc" <mc@community.nospam> wrote in message
news:44********@mail.hmgcc.gov.uk...
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

Jun 23 '06 #3
mc
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.

<asp:DropDownList ID="blah" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
Mike
Jun 23 '06 #4
mc
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" <mc@community.nospam> wrote in message
news:44********@mail.hmgcc.gov.uk...
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
Jun 23 '06 #5
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 (wa****@online.microsoft.com, 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.

Jun 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
2
by: Bill Manring | last post by:
In an ASP.NET web application, I am using the standard list box server control and I am adding items to with client side script. When the page posts back, the added items are not available on the...
2
by: Rich | last post by:
All of my efforts to add items to display in the combobox fail; I've added items using the IDE in VS2003 (String Collection Editor) or programtically and all I get is an empty drop down list that...
2
by: Samantha Penhale | last post by:
Hi, I have a drop down list ddlServers and in my data access layer I run a stored procedure to populate it. I need to be able to append items to the drop down list and don't know how to do this....
1
by: Aaron Prohaska | last post by:
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...
2
by: bmayer | last post by:
I am using a detail view and binding it to a sql data source (at some point in the future it will be an object data source). I have seen the article "Working with Data is ASP.NET 2.0 ::...
3
by: UJ | last post by:
Is there a way to have items in a drop down list where certain items are a different color? How about having items that are strikethrough? TIA - Jeff.
4
by: Pufferfish | last post by:
Hi everyone, I want to be able to select multiple items from a drop down list. I have set up a second table with a list of records under one field called "Courses". On my main database table I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.