我正在使用asp.net和c#.net中的文本框工作ajax auto completeextender。我无法获得可供选择的列表,我有适当的Web服务方法,名为..can任何人都可以指导我完成automo完成。 声明
这是我正在使用i aspx的标签
the ajax part
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtUsername" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetCountries" >
</asp:AutoCompleteExtender>
</div>
背后的代码
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string strUserName, int count)
{
SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
scon.Open();
SqlCommand scmd = new SqlCommand("select * from UserInformation where UserName like @Username+'%'", scon);
scmd.Parameters.AddWithValue("@Username", strUserName);
SqlDataAdapter sda = new SqlDataAdapter(scmd);
DataTable dt = new DataTable();
sda.Fill(dt);
List<string> UserNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
UserNames.Add(dt.Rows[i][1].ToString());
}
return UserNames;
}
答案 0 :(得分:0)
标签中的ASPX代码。 GetCur:asmx文件中给出的名称。
$("#<%=txtSearch.ClientID %>").autocomplete({
source: function(request, response) {
$.ajax({
url: '<%=ResolveUrl("~/WebService.asmx/GetCur") %>',
data: "{ 'prefixText': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function(data) {
response($.map(data.d, function(item) {
return {
label: item,
val: item
}
}))
},
error: function(response) {
alert(response.responseText);
},
failure: function(response) {
alert(response.responseText);
}
});
},
});
});