AutoComplete Text Box using jQuery in Asp.net c#
In this article, I have done a code for auto compete textbox value from database value using the JQuery. For calling the database value I have used the web services. Suppose If we are using Ajax in our project then we also read this article, AJAX AutoCompleteExtender Example in ASP.Net
Database:-
Source code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>AutoComplete Text Box using jQuery in Asp.net c#</title>
<style type="text/css">
.ui-menu
{
list-style: none;
padding: 2px;
margin: 0;
display: block;
}
.ui-menu .ui-menu
{
margin-top: -3px;
}
.ui-menu .ui-menu-item
{
margin: 0;
padding: 0;
zoom: 1;
float: left;
clear: left;
width: 100%;
}
.ui-menu .ui-menu-item a
{
text-decoration: none;
display: block;
padding: .2em .4em;
line-height: 1.5;
zoom: 1;
}
.ui-menu .ui-menu-item a.ui-state-hover, .ui-menu .ui-menu-item a.ui-state-active
{
font-weight: normal;
margin: -1px;
}
</style>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$("#txtName").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetName",
data: "{'Name':'" + document.getElementById('txtName').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("error");
}
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server" />
</form>
</body>
</html>
Code behind (c#)
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Data.SqlClient;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<string> GetName(string Name)
{
List<string> Result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Top 10 Name from record where Name 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;
}
}
}
}
Out-Put:-
Download:-
Download this code for jquery files
AutoComplete Text Box using jQuery in Asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
8:18:00 PM
Rating:
No comments: