2010-06-10 50 views
1

問題:如何在按下回車鍵時在AjaxControlToolkit組合框中獲取用戶輸入的值?

用戶輸入的值在AjaxControlToolkit ComboBox的。文本屬性缺少當回車鍵按下。此外,「改變」事件不會被調用,但我不使用回發,所以我不在乎。

例子:

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 「組合框」 嵌入TextBox控件的。文本訪問由用戶輸入的值。

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(); 
} 

回答

0

我結束了創建實用的方法來解決這個問題,首先使用ComboBox.Text屬性之前要執行。由於AjaxToolKit組合框有一個下拉子組件,因此我需要檢查下拉列表以查看列表中是否已經存在新值,並在分配新文本值之前如果缺失則添加它。

//***************************************************************** 
// 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; 
     } 
    } 
} 
相關問題