AutoComplete Text in Textbox using JQuery in ASP.Net


One day my friend asked me, I was using Ajax AutoComplete Extender but now it gives me some version issues. So I want to go for some simple way to use Auto Complete text in TextBox.
I suggest him, go for the below code using JQuery.
Here I am posting the code, so it will help to all.

.ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextChange.aspx.cs" Inherits="WebApplication1.TextChange" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript">
    $(function () {
        SearchText();
    });
    function SearchText() {
        debugger;
        $("#txtSearch").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "TextChange.aspx/GetCountry",
                    data: "{'countryName':'" + $('#txtSearch').val() + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return item;
                            }));
                        }
                        else {
                            response(['No Records Found']);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
        });
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="demo">
<label for="tbAuto">Enter Country: </label>
<input type="text" id="txtSearch" />
</div>
    </form>
</body>
</html>

.CS Page:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TextChange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

            }
        }

        [WebMethod]
        public static List<string> GetCountry(string countryName)
        {
            List<string> result = new List<string>();
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("select countryName from Country where countryName LIKE ''+@Parameter+'%'", con))
                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@Parameter", countryName);
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(dr["countryName"].ToString());
                    }
                    return result;
                }
            }
        }

    }
}

Note: I have simply created on table in database name as "Country" with two column name as CountryId and Country Name.
The above code executed and when you run the page just enter the letter country name starts from.
I hope it will help you!
Cheers,


Comments