2013-05-16 40 views
0

我已經將此應用於輸入類型文本,但它不起作用。我需要一些指導來告訴我我錯在哪裏。這是JavaScript代碼,而不是工作:自動完成功能無法使用幫助是必需的

$(document).ready(function() { 
    $('#reasondescriptiontxtbox').autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "Default.aspx/getReason", 
       data: "{'keywords':'" + request.term + "'}", 
       dataType: "json", 
       async: true, 
       success: function (data) { 
        response(data.d); 
       }, 
       error: function (result) { 
        //alert("Error"); 
       } 
      }); 
     }, 
     minLength: 2 
    }); 
}); 

後面的代碼:

[WebMethod] 
public static IList<string> getReason(string keywords) 
{ 
    int count = 0; 
    IList<string> result = new List<string>(); 
    string constr 
     = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; 
    SqlConnection con1 = new SqlConnection(constr); 
    SqlCommand cmd1 = con1.CreateCommand(); 
    cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description '%" + keywords + "%'"; 

    try 
    { 
     con1.Open(); 
     SqlDataReader dr = cmd1.ExecuteReader(); 

     while (dr.Read()) 
     { 
      count++; 
      result.Add(dr["Code_Description"].ToString()); 

      if (count == 100) 
       break; 
     } 

     con1.Close(); 

     return result; 
    } 
    catch 
    { 
     return null; 
    } 
} 

我是否需要添加某種jQuery的文件?

+1

什麼是不工作?您的網絡方法是不是被稱爲?你有錯誤嗎? –

+0

不,我沒有得到任何錯誤,但自動完成功能不起作用我不知道問題在哪裏,爲什麼我提交了整個代碼 –

+0

是否調用了getReason方法?它是否返回'null'以外的任何東西? – Yoav

回答

0

我喜歡添加關鍵字這也是一個問題,其次,我改變這樣的:$('#reasondescriptiontxtbox').autocomplete( 這樣:$('input[id$=reasondescriptiontxtbox]').autocomplete( ,現在它工作完全正常

2

您的SQL代碼在where Code_Description '%" + keywords + "%'"中似乎缺少like,這可能是原因嗎?

你不會得到任何結果,並且可能會被Catch掩蓋的SQL異常。

嘗試改變該行

cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description like '%" + keywords + "%'"; 
+0

thanx Klors我添加了像關鍵字這也是一個問題,其次我改變了這個:$('#reasondescriptiontxtbox')。autocomplete(對此:$('input [id $ = reasondescriptiontxtbox]')。autocomplete(現在是工作完全正常 –

1

如果您使用的母版頁,然後你的JavaScript代碼應該是這樣的 $( '#<%= textbox1.ClientID%>')。自動完成({

,並確保你已經包括的jquery.js

+0

thanx我改變了這個:$('#reasondescriptiontxtbox')。autocomplete(to this:$('input [id $ = reasondescriptiontxtbox]')。自動完成(現在它工作得很好) –

+0

嘿標記爲答案這樣其他人也可以知道這一點。 –

0

我會組織的單元測試到你的項目,所以你可以測試在隔離getReason,與嘲笑輸入的任何版本。然後,你可以把和conque確定問題出在哪裏。如果問題出在客戶端,則需要將調試器附加到瀏覽器,並在您的處理程序中設置斷點以檢查本地和執行流。