Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

AutoCompleteExtender in ASP.Net

Viewed:  2533 
Posted On:  24/02/2013 20:25:32 
How to create an AutoComplete extender in ASP.NET application which calling through WebService from database. 

AutoComplete is an ASP.NET AJAX extender that can be attached to any TextBox control, and will associate that control with a popup panel to display words that begin with the prefix typed into the textbox.


Image 1.

Below is my ASPX..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>AutoCompleteExtender in ASP.NET</title>
    <form id="form1" runat="server">
        <table cellpadding="4" cellspacing="4" width="50%" align="center" border="1">
                    <asp:ScriptManager ID="ScriptManager1" runat="server">
                    Your Country Name:
                    <asp:TextBox ID="txtCountrySearch" runat="server"></asp:TextBox>
                    <cc1:AutoCompleteExtender ServiceMethod="SearchCountry" MinimumPrefixLength="1" CompletionInterval="100"
                        EnableCaching="false" CompletionSetCount="10" TargetControlID="txtCountrySearch"
                        ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">

Now my aspx.cs code:

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.Collections.Generic;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    public static List<string> SearchCountry(string prefixText, int count)
        using (SqlConnection conn = new SqlConnection())
            conn.ConnectionString = @"Data Source=.;Initial Catalog=MyDb;Integrated Security=true";
            using (SqlCommand cmd = new SqlCommand())
                cmd.CommandText = "select CountryName from Country where " +
                "CountryName like @SearchText + '%'";
                cmd.Parameters.AddWithValue("@SearchText", prefixText);
                cmd.Connection = conn;
                List<string> country = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                    while (sdr.Read())

                return country;

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us