<script type="text/javascript">
$(function () {
$("[id$=txtAuto]").autocomplete({
source: function (request, response) {
$.ajax({
url: "NameList.asmx/GetNameList",
data: "{ 'Name': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
async: true,
success: function (data) {
var Details = [];
for (i = 0; i < data.d.length; i++) {
Details[i] = data.d[i].Name;
} response(Details);
},
error: function (result) {
}
});
}
});
});
</script>
public class NameList : System.Web.Services.WebService
{
[WebMethod]
public List<UserNameList> GetNameList(string Name)
{
var emp = new UserNameList();
var fetchName = emp.GetEmpList()
.Where(m => m.Name.ToLower().StartsWith(Name.ToLower()));
return fetchName.ToList();
}
}
public class UserNameList
{
public int ID { get; set; }
public string Name { get; set; }
public List<UserNameList> GetEmpList()
{
List<UserNameList> emp = new List<UserNameList>();
string QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
using (SqlConnection obj_SqlConnection = new SqlConnection(QueryString))
{
using (SqlCommand obj_Sqlcommand = new SqlCommand("Select DISTINCT name as txt from lib_memtable where name like @SearchText +'%' ", obj_SqlConnection))
{
obj_SqlConnection.Open();
obj_Sqlcommand.Parameters.AddWithValue("@SearchText", Name);
SqlDataReader obj_result = obj_Sqlcommand.ExecuteReader();
while (obj_result.Read())
{
emp.Add(obj_result["name"].ToString().TrimEnd());
}
}
}
return emp;
}
- 这段代码有什么问题?从过去的两个小时开始,我一直在研究这个问题。它没有从数据库中获取名称。
- 我想这有什么问题
emp.Add(obj_result["name"].ToString().TrimEnd());