[英]auto complete on asp.net control
嘿,我想在asp.net上的default1.aspx頁面上使用的控件上使用自動完成jquery。 我的控件是Registration文件夾中的searchinput.ascx。 我的問題是我已經在searchinput控件的代碼文件上編寫了web方法(getmylist)。 但是永遠不會調用該方法。 誰能幫我
您可以在這里找到所需的wat。 另外,請顯示您的ajax呼叫,以便我可以嘗試幫助y它無法正常工作。 否則,您編寫Web方法並從jquery自動完成進行ajax調用的方法應該可以解決。
沒有代碼很難幫助您,但是一些常見的原因可能是:
您沒有正確使用ClientID值-asp.net控件在實際標記中沒有與設計器中相同的id
您的網絡方法中有錯誤-您應按f12鍵打開網絡開發人員工具欄,然后轉到NET選項卡(至少在firefox中),查看是否返回了500個錯誤代碼或類似的代碼
創建一個如下所示的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
});
});
});
這是工作示例……希望這可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.