当按下回车键时,如何在AjaxControlToolkit ComboBox中获取用户输入的值?

时间:2010-06-10 20:30:15

标签: c# asp.net asp.net-ajax

问题:

按下回车键时,AjaxControlToolkit ComboBox的.Text属性中缺少用户输入的值。此外,“on change”事件事件未被调用,但我仍然没有使用回发,所以我不在乎。

示例:

private void BuildFileListDetails(NHibernateDataProvider _providerM)  
{  
    int resultsPage = Convert.ToInt32(ddlNavPageNumber.Text);  
    const int RESULTS_PAGE_SIZE = 100;  

    // The cbFileName.Text equals "" Not what user entered  
    string searchFileName= cbFileName.Text;  

    var xrfFiles = _providerM.GetXrfFiles(searchFileName, resultsPage, RESULTS_PAGE_SIZE);

    gvXrfFileList.DataSource = xrfFiles;

    gvXrfFileList.DataBind();

}

我的解决方案:

我需要访问AjaxToolkit“ComboBox”嵌入的TextBox控件的.Text来访问用户输入的值。

private void BuildFileListDetails(NHibernateDataProvider _providerM)
{

    int resultsPage = Convert.ToInt32(ddlNavPageNumber.Text); 
    const int RESULTS_PAGE_SIZE = 100;
    string searchFileName;

    //The Solution: Access the AjaxToolkit "ComboBox" imbedded TextBox control's .Text to access the value entered by user.
    TextBox textBox = cbFileName.FindControl("TextBox") as TextBox;
    if (textBox != null)
    {
       searchFileName = textBox.Text; //textBox.Text = "User Entered Value"
    }

    var xrfFiles = _providerM.GetXrfFiles(searchFileName, resultsPage, RESULTS_PAGE_SIZE);
    gvXrfFileList.DataSource = xrfFiles;
    gvXrfFileList.DataBind();
}

1 个答案:

答案 0 :(得分:0)

我最终创建了实用方法来解决此问题,在首次使用ComboBox.Text属性之前执行。由于AjaxToolKit ComboBox有一个下拉子组件,我需要检查下拉列表以查看列表中是否已存在新值,如果缺少该值,则在分配新文本值之前添加它。

//*****************************************************************
// 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;
        }
    }
}