954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Avoid page postback

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

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

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
Sarama2030
Junior Poster in Training
74 posts since May 2009
Reputation Points: 10
Solved Threads: 4
 

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

Sarama2030
Junior Poster in Training
74 posts since May 2009
Reputation Points: 10
Solved Threads: 4
 

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

Sarama2030
Junior Poster in Training
74 posts since May 2009
Reputation Points: 10
Solved Threads: 4
 

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);
        }
    }
bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You