AjaxControlToolkit ComboBox.Text User Entered Value is Missing When Enter Key is Pressed also the on change events are not called

by jasonvonruden 10. June 2010 04:55

The Issue

The AjaxControlToolkit ComboBox has a .Text attribute and a couple of server side events OnSelectedIndexChanged and OnTextChanged to deal with field changing.

However  the user entered value is missing in the .Text attribute on an enter key press and the “on change” events events are not called.

 

Issue Code Example:

private void BuildFileListDetails(NHibernateDataProvider _providerM)
{
    int resultsPage = Convert.ToInt32(ddlNavPageNumber.Text); 
    const int RESULTS_PAGE_SIZE = 100;
 
    //The Problem: AjaxControlToolkit ComboBox.Text User Entered Value Missing When Enter Key Pressed.
    string searchFileName= cbFileName.Text;  // The cbFileName.Text equals ""
 
    var xrfFiles = _providerM.GetXrfFiles(searchFileName, resultsPage, RESULTS_PAGE_SIZE);
    gvXrfFileList.DataSource = xrfFiles;
    gvXrfFileList.DataBind();
}

The Solution

You need to access the AjaxToolkit "ComboBox" imbedded TextBox control's .Text to access the value entered by user.

I ended up creating Utility Method to fix this issue that is executed before first use of the ComboBox.Text property. Since the AjaxToolKit ComboBox has a drop down sub component, I needed to check the drop down list to see if the new value already exists in the list and add it if it is missing before assigning the new text value.

Solution Code Example:

//*****************************************************************
// Fix AjaxToolKit ComboBox Text when Enter Key is pressed bug.
//*****************************************************************
public void FixAjaxToolKitComboBoxTextWhenEnterKeyIsPressedIssue(AjaxControlToolkit.ComboBox _combobox)
{
  TextBox textBox = _combobox.FindControl("TextBox") as TextBox;
  if (textBox != null)
  {
      if (_combobox.Items.FindByText(textBox.Text) == null)
      {
          _combobox.Items.Add(textBox.Text);
      }
      _combobox.Text = textBox.Text;
  }
 
 
/*****************************************************************
/ Build File List Details.
/*****************************************************************
rivate void BuildFileListDetails(NHibernateDataProvider _providerM)
 
   int resultsPage = Convert.ToInt32(ddlNavPageNumber.Text); 
   const int RESULTS_PAGE_SIZE = 100;
 
   FixAjaxToolKitComboBoxTextWhenEnterKeyIsPressedIssue(cbFilename);
   string searchFileName = cbFileName.Text;
 
   var xrfFiles = _providerM.GetXrfFiles(searchFileName, resultsPage, RESULTS_PAGE_SIZE);
   gvXrfFileList.DataSource = xrfFiles;
   gvXrfFileList.DataBind();
 

Tags:

ASP.NET