Hi,
In my page their is one combobox and one listbox. When i select an item in combobox then selected item added to listbox. Combobox has ispostback property to true.
Now my question is,
how to check item is already present in listbox on combobox selected item changed event.


Thanks in advance

The bottom line is to compare strings.Take the selected item in the combo box and compare it with all the strings in the list box.I am guessing you're conversant with programming in asp.net i.e vb.net or c#.I'll give u a code snippet in vb.net:

'assuming combo box=cbocombo and list box=lstlist
'this code runs in the combo box selectedindex changed event

Dim selectedstr as string=cbocombo.selecteditem.tostring

For i as integer= 0 to lstlist.items.count-1

Next

The bottom line is to compare strings.Take the selected item in the combo box and compare it with all the strings in the list box.I am guessing you're conversant with programming in asp.net i.e vb.net or c#.I'll give u a code snippet in vb.net:

'assuming combo box=cbocombo and list box=lstlist
'this code runs in the combo box selectedindex changed event

Dim selectedstr as string=cbocombo.selecteditem.tostring

For i as integer= 0 to lstlist.items.count-1
   if lstlist.items(i).tostring=selectedstr then
       exit sub      
   Then

Next

lstlist.items.add(selectedstr)

.

Its something close to this but i'm not sure on the syntax for listbox methods the IDE
can help out hopefully

there'a a typo in the code the if in the for loop should be closed by and end if not a then

Thanks sarama2030 for your solution i have just modified it. It works fine now thanks once again.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Boolean ischeck = false;
        string selectedstr = DropDownList1.SelectedValue;
        if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; (i <= (ListBox1.Items.Count - 1)); i++)
            {
                if ((ListBox1.Items[i].ToString() == selectedstr))
                {
                    ischeck = true;
                }                              
            }
        }
        else
        {
            ischeck = true;
            ListBox1.Items.Add(selectedstr);
        }
        if (ischeck == true)
        {
            return;
        }
        else
        {
            ListBox1.Items.Add(selectedstr);
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.