2013-05-16 39 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确定问题出在哪里。如果问题出在客户端,则需要将调试器附加到浏览器,并在您的处理程序中设置断点以检查本地和执行流。