hey I want to use autocomplete jquery in asp.net on a control which is used on a default1.aspx page. My control is searchinput.ascx which is in Registration folder. My ploblem is I have written web method (getmylist) on code file of searchinput control. but that method is never called. can anyone help me
问问题
288 次
3 回答
0
没有代码很难帮助您,但一些常见原因可能是:
您没有正确使用 ClientID 值 - asp.net 控件在实际标记中的 id 与在设计器中的不同
您的 Web 方法有错误 - 您应该按 f12 打开您的 Web 开发人员工具栏并转到 NET 选项卡(至少在 Firefox 中)查看是否返回 500 错误代码或类似代码
于 2012-11-01T13:38:53.760 回答
0
您可以从那里找到您需要的水。另外,请显示您的 ajax 调用,以便我可以尝试帮助您解决它不起作用。否则,您编写 Web 方法并从 jquery 自动完成进行 ajax 调用的方法应该可以正常工作。
于 2012-11-01T13:27:55.750 回答
0
创建如下 Web 方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetPatientFirstName(string prefix)
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
string connectionstring = CCMMUtility.GetCacheForWholeApplication();
conn.ConnectionString = connectionstring;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select distinct top(10) PatientFirstname from tblMessage where " +
"PatientFirstname like '%'+ @SearchText + '%' order by PatientFirstname";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}", sdr["PatientFirstname"]));
}
}
conn.Close();
}
return customers.ToArray();
}
}
这是html代码:
$(document).ready(function () {
$('[ID$=txtPatientFirstname]').live('keyup.autocomplete', function () {
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Resources/WebService.asmx/GetPatientFirstName") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
},
minLength: 1
});
});
});
这是工作示例......希望这能解决您的问题..
于 2012-11-01T13:46:28.330 回答