0
votes

I have a listbox which contain 6 items . In the seleceted index changed event of list box text of label is changed to the selected item value.Select mode of listbox is multiple.I want When a user select an item it should remove from listbox and add to label text. user can remove the selected items 1 2 or all I am not using textbox because i want only listbox items can be select. my html is

<asp:Label ID="lbl_mar_cat" runat="server" Width="100%" Font-Size="Small"></asp:Label>
<asp:ListBox ID="listbox_mar" runat="server" SelectionMode="Multiple" 
    CssClass="listbox" AutoPostBack="True" 
    onselectedindexchanged="BulletedList1_SelectedIndexChanged" >
    <asp:ListItem Value="Doesn't Matter">Doesn't Matter</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>Divorced</asp:ListItem>
</asp:ListBox>

my server side code is

 if (listbox_mar.SelectedValue == "Doesn't Matter")
    {
        lbl_mar_cat.Text = "Doesn't Matter";
    }
    else
    {
        if (lbl_mar_cat.Text == "")
        {
            lbl_mar_cat.Text = listbox_mar.SelectedValue.ToString();
        }
        else
        {
            lbl_mar_cat.Text += ", " + listbox_mar.SelectedValue.ToString();
        }
    }
1
Show your work first..Soner Gönül

1 Answers

1
votes

Try this:

protected void listbox_mar_SelectedIndexChanged(object sender, EventArgs e)
{
    for (int i = 0; i < listbox_mar.Items.Count; i++)
    {
        if (listbox_mar.Items[i].Selected)
        {
            lbl_mar_cat.Text += listbox_mar.Items[i].Text+ " , " ;
            listbox_mar.Items.Remove(listbox_mar.Items[i]);
        }
    }       
}