0

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.. Commented Sep 28, 2013 at 10:52

1 Answer 1

1

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]);
        }
    }       
}
Sign up to request clarification or add additional context in comments.

1 Comment

sir can this done without any button click because i dont use any button. Should I use textbox in place of label? I just want to remove the items by clicking on them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.