Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » How do I » Auto Complete in ASP.NET using Ajax

Auto Complete in ASP.NET using Ajax

This article will show you how to use Ajax AutoComplete control.

Author Rank :
Page Views : 65739
Downloads : 2739
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
AJAXAutoCompleteExtender.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction:

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

Create a new website using ASP.NET Ajax-Enabled Web Site:



Now drag and drop three controls on page. ScriptManager, Textbox, AutoCompleteExtender.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <div>

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

            <cc1:AutoCompleteExtender ID="AutoCompleteExtender1"

            runat="server"

            ServicePath="WebService.asmx"

            ServiceMethod="FindName"

            MinimumPrefixLength="1"

            TargetControlID="TextBox1">

            </cc1:AutoCompleteExtender>

        </div>

    </form>

Now add a new WebService class:


 
Here is web service code.

Imports System

Imports System.Collections.Generic

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Data.SqlClient

Imports System.Data

Imports System.Data.Common

Imports AtlasTextBoxAutoComplete.DL

Imports System.Collections

 

<WebService(Namespace:="http://tempuri.org/")> _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<System.Web.Script.Services.ScriptService()> _

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class WebService

    Inherits System.Web.Services.WebService 

    <WebMethod()> _

        <System.Web.Script.Services.ScriptMethod()> _

        Public Function FindName(ByVal prefixText As String, ByVal count As Integer) As String()

        'return all records whose Title starts with the prefix input string

        Dim titleArList As New List(Of String)()

        Dim drProducts As SqlDataReader = sqlProductProvider.GetNameList(prefixText)

        While drProducts.Read()

            Dim strTemp As String = Convert.ToString(drProducts("Name"))

            titleArList.Add(strTemp)

        End While

        Return titleArList.ToArray()

    End Function

End Class

Result will look like this:


 
I am adding two classes in App_Code folder for database connectivity. You shoudl change your web.config key for database connection string. I am attaching my database in App_Code folder.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Raj Kumar on Nov 03, 2010
Here is web service code in C#.


using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Data.SqlClient; 
using System.Data; 
using System.Data.Common; 
using AtlasTextBoxAutoComplete.DL; 
using System.Collections; 

public class WebService : System.Web.Services.WebService 
{  
    
    public string[] FindName(string prefixText, int count) 
    {        
        //return all records whose Title starts with the prefix input string         
        List<string> titleArList = new List<string>();         
        SqlDataReader drProducts = sqlProductProvider.GetNameList(prefixText);         
        while (drProducts.Read()) {            
            string strTemp = Convert.ToString(drProducts("Name")); 
            titleArList.Add(strTemp); 
        }   
        return titleArList.ToArray(); 
    } 
    
 [Top] Rate this article
 
 About the author
 
Raj Kumar

Raj Kumar is a Microsoft MVP and Senior Software Engineer with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, SQL Server 2005/2008, Oracle, WPF, WCF, XAML and Silverlight. He has over 6 years of IT experience working most on Microsoft technologies. He holds Master's degree in Computer Science. When he is not writing code, he likes to write articles and play cricket.

Reach him at raj2511984@gmail.com 

 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
hiii by bhrugesh On February 7, 2009
gv me same code in c#
Reply | Email | Modify 
Re: hiii by Raj On April 30, 2009
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using AtlasTextBoxAutoComplete.DL;
using System.Collections;

public class WebService : System.Web.Services.WebService
{  
    
    public string[] FindName(string prefixText, int count)
    {        
        //return all records whose Title starts with the prefix input string         
        List<string> titleArList = new List<string>();         
        SqlDataReader drProducts = sqlProductProvider.GetNameList(prefixText);         
        while (drProducts.Read()) {            
            string strTemp = Convert.ToString(drProducts("Name"));
            titleArList.Add(strTemp);
        }   
        return titleArList.ToArray();
    }
    
}
Reply | Email | Modify 
Re: hiii by Raj On April 30, 2009
you can use this code converter.

http://www.developerfusion.com/tools/convert/vb-to-csharp/
Reply | Email | Modify 
hey by travis On April 27, 2009
where does it read from? a database
Reply | Email | Modify 
Re: hey by Raj On June 7, 2010
yes web service is reading from database.
Reply | Email | Modify 
VB? by Nisse On February 3, 2010

The site is "c-sharpcorner" and your eample is in VB???

Reply | Email | Modify 
Re: VB? by Raj On June 7, 2010
yes u r right. by mistake.
Reply | Email | Modify 
asking about ajax by Shivam On May 25, 2010
I want to know that can we use Ajax without downloading any additional tool or compiler in .net 3.5. If so then How?????
Plz do help me i really need it for  submission of my project.
Reply | Email | Modify 
Re: asking about ajax by Raj On June 7, 2010
if you want to use ajax then u have to download ajax extensions. there are no other way i guess.
Reply | Email | Modify 
from where these records are coming by Pushpendra Kumar Tripathi On August 16, 2010
when we enter any letter how come the data is retrieving
Reply | Email | Modify 
Re: from where these records are coming by Raj On August 18, 2010
data is coming from database according to typed character. download sample application and change connection string, you will understand how it works.
Reply | Email | Modify 
how to retrieve selected Text value?? by Ahmad On September 19, 2010
Hello,

Your sample rocks! despite I need to retrieve selected users id(for ID entry purpose) i.e. I need to display name but I will insert id of select employee.

Can you assisst me on this!!! secondly I need to display employee name and employee id respectively but once you select it will only display employee name..is there any way???

Thanks in advance

plz reply on azac_81@yahoo.com
Reply | Email | Modify 
Thanks by black On November 2, 2010
Thanks
Reply | Email | Modify 
Code Display in vb.net by Rajai On January 28, 2011
Hello...Sir this code garnet in vb.net
Reply | Email | Modify 
C#.net3.5 autocomplete within gridview by Amit On February 4, 2011
this is a very nice article C#.net3.5 autocomplete within gridview but wait... i have a table (named- ItemInformation and fields id(PK),ItemCode , Name , Color,Price,Quantity). Now on .aspx page within the gridview(which is empty on form load) i have some textboxes for ItemID,ITemCode,color, Name,Price,Quantity etc.with a button to add a new row. Now i want to apply autocomplete on ITemCode textbox and after selecting from that autocomplete http://www.c-sharpcorner.com/AdRedirector.aspx?BannerId=532&target=http://www.discountasp.net/tfs/go/go.aspx?i=14951values, the other textbox fields such as(name,id ,color,price )are automatically filled regarding to that selected value. plz help me.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.