tags:

views:

4368

answers:

10

I'm using the new Microsoft ASP.NET Ajax Combobox control in my web application and I'm having trouble figuring out how to programatically set the selected list item. For example, I have a form to accept addresses and a combobox to accept a city name. After the user enters and saves and address, I'd like to reset the form, including the comboboxes.

With a regular ASP.NET dropdownlist control I can reset the selected item like below:

City.SelectedIndex = -1;

The ASP.NET AJAX Combobox control, accepts this line of code, but when the page posts back, the previously selected value is still selected. Help would be appreciated.

Update: I tried the following to no avail...

City.ClearSelection();
A: 

Try:

ComboBox.Text = "";  
ComboBox.ClearSelection();
Coov
Just tried that and no luck. Previously selected value is still there. Only solution is to re-bind the list, but I'm trying to avoid another database hit.
jwalkerjr
It there an items collection with a clear method? ComboBox.Items.Clear()?
Coov
Yes, and that does clear the list items, BUT the selected value STILL displays in the box.
jwalkerjr
A: 

Are you referring to the CascadingDropDown? If so, set the SelectedValue of the CascadingDropDown to null.

CascadingDropDown1.SelectedValue = null;
Tim
Tim, actually I'm talking about the new MS Ajax Control Toolkit's ComboBox control. Unfortunately, setting the .SelectedValue property of the control to null still doesn't work. Convinced at this point this is a bug in the control. Wish MS would confirm it, though.
jwalkerjr
+1  A: 

This is an open issue on CodePlex. So, because it's a bug, there's no solution until a patch is issued. Here's the issue on CodePlex.

jwalkerjr
A: 

hi

I also have the same problem.

I have two combo boxes cboCity and cboState whenever I selected city from cboCity then I change State from cboState then I want to set city combo blank I tried following methods-

cboCity.SelectedIndex = 0

cboCity.SelectedIndex = -1

cboCity.SelectedValue = string.Empty

cboCity.SelectedIndex = 0

cboCity.ClearSelection()

cboCity.Items.Clear()

but nothing is working it always shows previous selected value.

Please help

Thanks

Prakash Gupta

+1  A: 

it's work when I use DropDownStyle="DropDown" and this command to clear text combobox1.SelectedItem.Text = ""

A: 

ok so if you want to set the index it is actually stored in the hidden field in the control it looks like. So what i am doing to clear it is this.

foreach (Control control in cbFeatures.Controls)
            {
                if (control is HiddenField)
                    ((HiddenField)control).Value = "0";
            }

This is working great for me where the value is the index that you want to set.

A: 

This can be solved by clearing the hidden fields as explained in the following blog http://techiecentre.blogspot.com/

VInaya
A: 

Gr8 it is solved for me in using the for loop hidden

Murali
A: 

Replace City.ClearSelection() with the following:

City.Items.Insert(0,"");
City.SelectedIndex = 0;

Now this will put an empty string in the drop down list part of the City control as well as setting the text to empty string.

To remove the empty string from the drop down list use this:

City.Items.Remove("");

in the City Page_Load event.

Joey
A: 

One Solution for this is use Following 2 Commands Before you load page Second time or make it visible in update Panel

            Combobox1.ClearSelection()
            Combobox1.Dispose()

This will Clear Combobox Value.

Hope this Helps. Took me a while to find it.

Bhushan Pachpande

related questions