Hi ,
Refer the Below Articles links also refer the below code for your reference.
- Display No results found message (No match found message) in jQuery AutoComplete
HTML
Default.aspx
Enter search term:
<input type="text" id="txtSearch" />
<div>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
<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/GetSearchResults',
data: "{ 'prefix': '" + 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,
val: item
};
}))
} else {
response([{ label: 'No results found.', val: -1}]);
}
}
});
},
select: function (e, u) {
if (u.item.val == -1) {
return false;
}
}
});
});
</script>
</div>
Code
Default.aspx.cs
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static string[] GetSearchResults(string prefix)
{
List<string> searchResult = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = @"SELECT FirstName Result FROM Employees
WHERE FirstName like @SearchText + '%'
UNION ALL
SELECT City Result FROM Employees
WHERE City Like @SearchText + '%'
UNION ALL
SELECT HomePhone Result FROM Employees
WHERE HomePhone like '%' + @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
searchResult.Add(sdr["Result"].ToString());
}
}
conn.Close();
}
}
return searchResult.ToArray();
}
Screenshot