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

Discussion in 'ASP .Net' started by mc, Jun 23, 2006.

  1. mc

    mc Guest

    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
     
    mc, Jun 23, 2006
    #1
    1. Advertising

  2. mc

    Jared Guest

    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
     
    Jared, Jun 23, 2006
    #2
    1. Advertising

  3. mc

    Winista Guest

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


    "mc" <> wrote in message
    news:449be44f$...
    > 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
     
    Winista, Jun 23, 2006
    #3
  4. mc

    mc Guest

    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
     
    mc, Jun 23, 2006
    #4
  5. mc

    mc Guest

    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" <> wrote in message
    > news:449be44f$...
    >
    >>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
     
    mc, Jun 23, 2006
    #5
  6. 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 (, 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.
     
    Walter Wang [MSFT], Jun 26, 2006
    #6
    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. SirPoonga
    Replies:
    2
    Views:
    799
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,047
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    295
  5. Replies:
    3
    Views:
    312
Loading...

Share This Page