Question

I have a two listboxes and a button. I need to add selected item from one listbox to another with click of button.

enter image description here

and here is the code of the button

protected void ASPxButton4_Click(object sender, EventArgs e)
    {
        if (listBoxSubeKiyaslama1.SelectedIndex > -1)
        {
            listBoxSubeKiyaslama2.Items.Add(listBoxSubeKiyaslama1.SelectedItem);
            listBoxSubeKiyaslama2.Items.RemoveAt(listBoxSubeKiyaslama1.SelectedIndex);
            listBoxSubeKiyaslama2.UnselectAll();
        }
    }

when I click the button, I see that listBoxSubeKiyaslama1.SelectedIndex is always "-1". because I think it postbacks and clears items from the first listbox. How can I fix this?

Was it helpful?

Solution

Can you try the below code:

    if (listBoxSubeKiyaslama1.SelectedItem != null)
    {
        listBoxSubeKiyaslama2.Items.Add(listBoxSubeKiyaslama1.SelectedItem);
        listBoxSubeKiyaslama2.Items.RemoveAt(listBoxSubeKiyaslama1.SelectedIndex);
        listBoxSubeKiyaslama2.UnselectAll();
    }

The controls will be not save the values on postbacks if EnableViewState = false. By default it is true. Please make sure you are not setting it to false.

I also suggest you to put your control in UpdatePanel to avoid full postback.

Like:

<asp:UpdatePanel ID="up1" runat="Server">
    <ContentTemplate>
        <asp:ListBox ID="listBoxSubeKiyaslama1" runat="server">
        </asp:ListBox>
        <asp:ListBox ID="listBoxSubeKiyaslama2" runat="server">
        </asp:ListBox>
    </ContentTemplate>
</asp:UpdatePanel>

OTHER TIPS

Try the following code:-

ASPX.CS

public string GetSelectedItems(ListBox control)
    {
        var items = new StringBuilder();
        foreach (ListItem item in control.Items)
        {
            if (item.Selected)
                items.Append(string.Format("{0},", item.Value));
        }
        return items.ToString().Trim().TrimEnd(',');
    }
    protected void btnMoveRight_Click(object sender, EventArgs e)
    {
        for (int i = lbCourses1.Items.Count - 1; i >= 0; i--)
        {
            if (lbCourses1.Items[i].Selected == true)
            {
                lbCourses2.Items.Add(lbCourses1.Items[i]);
                ListItem li = lbCourses1.Items[i];
                lbCourses1.Items.Remove(li);
            }
        }
    }
    protected void btnMoveLeft_Click(object sender, EventArgs e)
    {
        for (int i = lbCourses2.Items.Count - 1; i >= 0; i--)
        {
            if (lbCourses2.Items[i].Selected == true)
            {
                lbCourses1.Items.Add(lbCourses2.Items[i]);
                ListItem li = lbCourses2.Items[i];
                lbCourses2.Items.Remove(li);
            }
        }
    }

    var selectedValues =  GetSelectedItems(lb2);

ASPX

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
            <asp:Label ID="lbl1" runat="server" Text="lbl1:"></asp:Label>
            <asp:ListBox ID="lb1" runat="server" SelectionMode="Multiple"></asp:ListBox>
            <asp:Button Runat="server" ID="btnMoveRight" Text=">>" 
            onclick="btnMoveRight_Click"></asp:Button>
            <asp:Button Runat="server" ID="btnMoveLeft" Text="<<" 
            onclick="btnMoveLeft_Click"></asp:Button>
            <asp:ListBox ID="lb2" runat="server" SelectionMode="Multiple"></asp:ListBox>
        </div>
</ContentTemplate>
</asp:UpdatePanel>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top