Mein Default.aspx-Code:
Code: Select all
$(document).ready(function () {
SearchText();
});
function SearchText() {
$("#TextBox1").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetName",
data: "{'Name':'" + document.getElementById('TextBox1').value + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert("Error");
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('#lblUserId').text(ui.item.val);
}
});
}
Code: Select all
Code: Select all
[WebMethod]
public static List GetName(string Name)
{
List Result = new List();
using (SqlConnection con = new SqlConnection("Data Source=Myservername;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=myID; password=mypassword"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Select Model as Name from AAProduct_Details where Model LIKE '%'+@SearchEmpName+'%'";
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@SearchEmpName", Name);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Result.Add(dr["Name"].ToString());
}
con.Close();
return Result;
}
}
}