Ref:
Display No results found message (No match found message) in jQuery AutoComplete
Please refer this code
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
rel="Stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("[id*=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: 'Default.aspx/GetAutoCompleteData',
data: "{ 'username': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
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 results found.', val: -1}]);
}
}
});
},
select: function (e, u) {
if (u.item.val == -1) {
return false;
}
}
});
$("[id*=txtSearch1]").autocomplete({
source: function (request, response) {
$.ajax({
url: 'Default.aspx/GetAutoCompleteData1',
data: "{ 'username1': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
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 results found.', val: -1}]);
}
}
});
},
select: function (e, u) {
if (u.item.val == -1) {
return false;
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="span12">
<input type="text" id="txtSearch" />
<input type="text" id="txtSearch1" />
<button type="submit" style="margin-top: -11px;" class="btn btn-warning btn-large">
Search
</button>
</div>
</div>
</form>
</body>
</html>
Namespaces
using System.Data.SqlClient;
using System.Web.Services;
using System.Configuration;
C#
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select ContactName from Customers where ContactName LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["ContactName"].ToString());
}
return result;
}
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetAutoCompleteData1(string username1)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select ContactName from Customers where ContactName LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username1);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["ContactName"].ToString());
}
return result;
}
}
}