There are so many ways to create an Auto Complete TextBox in ASP.NET like using an Ajax AutoComplete Control with Web Service and WCF. But here we will see how to use jQuery UI autocomplete functionality in ASP.NET.
Pre-requisites
- Create the simple table tblEmp with Id and EmpName Field in SQL Server
- Download the Plugin in the local system or use the online plugin as in the following:
http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js
http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js
http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css"
You can see here what can you do from the Jquery-ui plugin.
http://jqueryui.com
Now we will see the AutoComplete functionality of the jQuery UI. In this sample I will use the online plugin.
Step 1: Create the aspx page with TextBox control and the jQuery required plugin in the Head portion of the HTML.
Step 2: Write the jQuery UI auto complete jQuery syntax with Ajax web method as given below.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxTest.WebForm1" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
- <link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
- <script type="text/javascript">
- $(function () {
- $("#txtEmpName").autocomplete({
- source: function (request, response) {
- var param = { empName: $('#txtEmpName').val() };
- $.ajax({
- url: "WebForm1.aspx/GetEmpNames",
- data: JSON.stringify(param),
- dataType: "json",
- type: "POST",
- contentType: "application/json; charset=utf-8",
- dataFilter: function (data) { return data; },
- success: function (data) {
- response($.map(data.d, function (item) {
- return {
- value: item
- }
- }))
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- var err = eval("(" + XMLHttpRequest.responseText + ")");
- alert(err.Message)
-
- }
- });
- },
- minLength: 2
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- EmpName : <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
- <br />
- <br />
- <br />
- </div>
- </form>
-
- </body>
- </html>
Step 3: Write the code in the code behind file using Webmethod like this:
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Web.Services;
-
- namespace AjaxTest
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
-
- [WebMethod]
- public static List<string> GetEmpNames(string empName)
- {
- List<string> Emp = new List<string>();
- string query = string.Format("SELECT EmpName FROM tblEmp WHERE EmpName LIKE '%{0}%'", empName);
- using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True"))
- {
- using (SqlCommand cmd = new SqlCommand(query, con))
- {
- con.Open();
- SqlDataReader reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- Emp.Add(reader.GetString(0));
- }
- }
- }
- return Emp;
- }
- }
- }
Summary
This article showed how to use a jQuery UI AutoComplete in ASP.NET using jQuery theme. Let me know if you have a better approach for this.