How do I check whether an item exists in Listbox in asp.net?

18,603

Solution 1

Try this...

string toMatch = drp_branch.SelectedItem.Value + "-" + txt_acc_no.Text;
ListItem item = ListBox1.Items.FindByText(toMatch);
if (item != null)
{
    //found
}
else
{
    //not found
}

Solution 2

you can use this for checking if item is exsits in list box or not ...

string checkitem = drp_branch.SelectedItem.Value + "-" + txt_acc_no.Text;

if ((ListBox1.Items.Contains(checkitem) == true))
{
   Response.Write("Item exists");
}
else
{
   Response.Write("Item not   exists");

} 
Share:
18,603
David John
Author by

David John

I am a beginner and a student

Updated on June 14, 2022

Comments

  • David John
    David John almost 2 years

    How do I check whether an item already exists in a Listbox?
    I am using VS 2008 ASP.NET 3.5 framework C#. I used the following code...

     if (ListBox1.Items.Contains(drp_branch.SelectedItem.Value + "-" + txt_acc_no.Text))
     {...}