ListBox

Clear ListBox



ListBox2.Items.Clear()


Add item to ListBox



protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lstAdd.Items.FindByText(txtAdd.Text) == null)
            {
                lstAdd.Items.Add(txtAdd.Text);
            }
            else
            {
                Response.Write("<script>alert('Already Existed.......!')</script>");
            }
        }
  

Remove Single and Multiple Items From Listbox



<body>

    <form id="form1" runat="server">
    <div>
   
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
            <asp:ListItem>A</asp:ListItem>
            <asp:ListItem>B</asp:ListItem>
            <asp:ListItem>C</asp:ListItem>
            <asp:ListItem>D</asp:ListItem>
            <asp:ListItem>E</asp:ListItem>
            <asp:ListItem>F</asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="DELETE" />
   
    </div>
    </form>
</body>


C# CODE



protected void Button1_Click(object sender, EventArgs e)

    {

        int SelItemCount = ListBox1.Items.Count;  // COUNT TOTAL ITEMS COUNT  



        for (int i = 0; i < SelItemCount; i++)

        {

            ListBox1.Items.Remove(ListBox1.SelectedItem);

        }

    }


Check Items Existed or Not in ListBox

lstAdd is Listbox id
txtAdd is textbox Id


protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lstAdd.Items.FindByText( txtAdd.Text) != null)
            {
                Response.Write("<script>alert('Already Existed.......!')</script>");
            }
          
        }

Add items manually.....
 

lstAdd.Items.Add("Tokyo");

Insert the string or object at the desired point in the list with the Insert method:

 lstAdd.Items.Insert(0, "Copenhagen");

// To remove item with index 0:

 lstAdd.Items.RemoveAt(0);

// To remove "Tokyo" item:

 lstAdd.Items.Remove("Tokyo");

 

No comments:

Post a Comment