aspx Textbox Search from sql server (like google search):
----------------------------------------------------------------------------------------------------
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
aspx page code:
-------------------------------
<script language="javascript" type="text/javascript">
$(function () {
$('#<%=txtcustname.ClientID%>').autocomplete({
source: function (request, response) {
$.ajax({
url: "enquiry.aspx/GetCompanyName",
data: "{ 'customer':'" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
});
});
</script>
<asp:TextBox ID="txtcustname" runat="server" CssClass="textboxAuto" ></asp:TextBox>
C# CodeBehibd
-------------------------
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<string> GetCompanyName(string customer)
{
mainclass objmc = new mainclass();
List<string> result = new List<string>();
using (SqlCommand cmd = new SqlCommand("select name from customer where name LIKE @SearchText+'%'", objmc.con))
{
objmc.con.Open();
cmd.Parameters.AddWithValue("@SearchText", customer);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["name"].ToString());
}
return result;
}
}
DEMO:
----------------------------------------------------------------------------------------------------
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
aspx page code:
-------------------------------
<script language="javascript" type="text/javascript">
$(function () {
$('#<%=txtcustname.ClientID%>').autocomplete({
source: function (request, response) {
$.ajax({
url: "enquiry.aspx/GetCompanyName",
data: "{ 'customer':'" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
});
});
</script>
<asp:TextBox ID="txtcustname" runat="server" CssClass="textboxAuto" ></asp:TextBox>
C# CodeBehibd
-------------------------
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<string> GetCompanyName(string customer)
{
mainclass objmc = new mainclass();
List<string> result = new List<string>();
using (SqlCommand cmd = new SqlCommand("select name from customer where name LIKE @SearchText+'%'", objmc.con))
{
objmc.con.Open();
cmd.Parameters.AddWithValue("@SearchText", customer);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["name"].ToString());
}
return result;
}
}
DEMO:
No comments:
Post a Comment