ListBox not returning correct SelectedIndex

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 4, 2005.

  1. I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
    selection and lbxDestination -> single selection).
    Then:
    a) button "Load":
    ListItem l0 = new ListItem("01");
    lbxDestination.Items.Add(l0); //1st goes to Destination Box
    ListItem l1 = new ListItem("02");
    lbxSource.Items.Add(l1);
    ListItem l2 = new ListItem("03");
    lbxSource.Items.Add(l2);
    ListItem l3 = new ListItem("04");
    lbxSource.Items.Add(l3);
    b) button "Insert" (to move an item from Source to Destination):
    foreach (ListItem li in lbxSource.Items)
    {
    if (li.Selected)
    {
    lbxDestination.Items.Add(li);
    lbxDestination.SelectedItem.Value = "01";
    li.Selected = false; //otherwise I got a runtime error 'multiple not
    allowed in s
    }
    }
    c) a "Show" button that shows the context of the Selected Destination Item:
    tbxText.Text = lbxDestination.SelectedItem.Text;

    And it's unbelievable, but tbxText.Text always shows "01", no matter what I
    selected.
    I had this in a real world application and since I couldn't believe it I
    wrote a small test-page and it behaves exactly the same.
    Is there a workaround to this ? Or am I doing something wrong ?
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 4, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Karl Seguin Guest

    your step (b) is your problem
    First off, chances are you want to create new instance of ListItems instead
    of simply copying the reference...so instead of

    add(li) do add(new ListItem(li.Text));

    secondly, when you do SelectedItem.Value = "01" you are telling it to take
    the selected item and putting it's value to 01....since you are copying
    references (as above) each new item IS selected (since it was selected in
    the previous checkbox) and you are overwriting its value to "01" hence no
    matter which you select, they all have the same value...

    your insert should look something like:

    foreach (ListItem li in lbxSource.Items) {
    if (li.Selected) {
    lbxDestination.Items.Add(new ListItem(li.Text));
    li.Selected = false;
    }
    }
    lbxDestination.SelectedIndex = 0;


    karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Carlo Marchesoni" <> wrote in
    message news:...
    > I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
    > selection and lbxDestination -> single selection).
    > Then:
    > a) button "Load":
    > ListItem l0 = new ListItem("01");
    > lbxDestination.Items.Add(l0); //1st goes to Destination Box
    > ListItem l1 = new ListItem("02");
    > lbxSource.Items.Add(l1);
    > ListItem l2 = new ListItem("03");
    > lbxSource.Items.Add(l2);
    > ListItem l3 = new ListItem("04");
    > lbxSource.Items.Add(l3);
    > b) button "Insert" (to move an item from Source to Destination):
    > foreach (ListItem li in lbxSource.Items)
    > {
    > if (li.Selected)
    > {
    > lbxDestination.Items.Add(li);
    > lbxDestination.SelectedItem.Value = "01";
    > li.Selected = false; //otherwise I got a runtime error 'multiple

    not
    > allowed in s
    > }
    > }
    > c) a "Show" button that shows the context of the Selected Destination

    Item:
    > tbxText.Text = lbxDestination.SelectedItem.Text;
    >
    > And it's unbelievable, but tbxText.Text always shows "01", no matter what

    I
    > selected.
    > I had this in a real world application and since I couldn't believe it I
    > wrote a small test-page and it behaves exactly the same.
    > Is there a workaround to this ? Or am I doing something wrong ?
    >
    Karl Seguin, Feb 4, 2005
    #2
    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. John Giblin

    Asp:ListBox and selectedindex

    John Giblin, Nov 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,097
    John Giblin
    Nov 26, 2003
  2. S. Justin Gengo

    ListBox SelectedIndex is always -1

    S. Justin Gengo, Feb 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,755
  3. =?Utf-8?B?TGll?=
    Replies:
    5
    Views:
    673
    =?Utf-8?B?TGll?=
    Dec 14, 2004
  4. =?Utf-8?B?QW5nZWw=?=

    Drop Down List not returning selectedIndex

    =?Utf-8?B?QW5nZWw=?=, Apr 7, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    949
    Craig Deelsnyder
    Apr 7, 2005
  5. Phuff
    Replies:
    7
    Views:
    653
    Jeff Dillon
    Apr 11, 2006
Loading...

Share This Page