如何检查asp.net中列表框中是否存在某个项?

时间:2011-11-12 07:43:12

标签: c# asp.net listbox

如何检查列表框中是否已存在某个项目?
我正在使用VS 2008 ASP.NET 3.5框架C#。我使用了以下代码......

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

2 个答案:

答案 0 :(得分:9)

试试这个......

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

答案 1 :(得分:0)

您可以使用它来检查项目是否存在于列表框中......

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");

} 
相关问题