Thursday, October 8, 2009

Clearing an ajax combobox in ASP.Net

An ajax combo-box  coming with 3.5 framework is not working the expected way when it comes in rebinding the combo-box and clearing the selection value. The normal combo-box.ClearSelection() wont work in this case. It is because the combo-box selection value is saved inside a hidden control which is not getting cleared. The above problem can be solved by including the following code snippet.


           combobox1.ClearSelection();
            foreach (Control control in combobox1.Controls)
            {
                if (control is HiddenField)
                    ((HiddenField)control).Value = "0"; //Where 0 is the value associated with the first selection item.
            }

3 comments:

  1. This works perfectly for me..How do we change the style of a ajax toolkit 3.5 combo box?

    ReplyDelete
  2. Excelent! Just what i was looking for! Thank you so much!

    ReplyDelete
  3. Would this bug could be also associated with the problem I am having? The problem is:
    If I remove all items from my combobox, there is no way that I can manually insert an item and make that one the selected one without getting an error:
    “combobox has a SelectedIndex which is invalid because it does not exist in the list of items. Parameter Name:value”.

    Right now I have something like this:

    cmbLevel1Desc.Items.Clear();
    int cmboboxLength = cmbLevel1Desc.Items.Count;

    for (int i = 0; i < cmboboxLength; i++)
    {
    cmbLevel1Desc.Items.RemoveAt(0);

    }
    cmbLevel1Desc.Items.Insert(0, "");
    cmbLevel1Desc.SelectedIndex = 0;

    Can anybody advice me on this?
    Thank you very much,

    Al

    ReplyDelete