Auto Complete textbox with Ajax in asp.net c#

 In this article, I learn about auto fill record in textbox in asp.net using Ajax, much time we think to how to search the record by particular character so we can use the below code to remove this.If we do not want to use AJAX then we can use web services AutoComplete Text Box using jQuery in Asp.net c#


Data Base:-




Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="autocompltedwithajax.aspx.cs"
    Inherits="autocompltedwithajax" %>

<!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 runat="server">
    <title>Auto Completed with ajax </title>
    <%@ register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
    <%@ register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:TextBox ID="txtrecord"  runat="server" TabIndex="0"></asp:TextBox>
        <ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtrecord"
            MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="5"
            ServiceMethod="GetRecord">
        </ajax:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>


Code Behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

public partial class autocompltedwithajax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [ScriptMethod()]
    [WebMethod]
    public static List<string> GetRecord(string prefixText)
    {

        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from record where name like @Name+'%'  ", con);
        cmd.Parameters.AddWithValue("@Name", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> GetRecord = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            GetRecord.Add(dt.Rows[i][1].ToString());
        }
        return GetRecord;
    }

}

Out-put:-

Auto Complete textbox with Ajax in asp.net c# Auto Complete textbox with Ajax in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 2:26:00 PM Rating: 5

3 comments:

  1. I want to do same thing in windows from application. please guide

    ReplyDelete
    Replies
    1. Autocomplete textbox in c# windows application


      http://neerajcodesolution.blogspot.in/2015/04/autocomplete-textbox-in-c-windows.html

      Delete
  2. Learn AJAX in ASP.NET with C# at Vision Technology Service.

    Watch: https://www.youtube.com/watch?v=sviOQRNKp5M

    To know more, call us at 7044083818 or 9830310488

    You may register with us at http://visiontechno.net/vlc/register/

    ReplyDelete

Powered by Blogger.