ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
ASP.Net AJAX Control Toolkit AutoCompleteExtender without using Web Services
Author Name: Mudassar Khan Published Date: January 25, 2010
Filed Under :
ASP.Net
 |
AJAX
Views: 1030
Here I am explaining, how to use AJAX AutoCompleteExtender Control directly with ASP.Net Web Page without using any web service.
Database
For this tutorial I am making use of NorthWind database that you can download using the link given below.
Download Northwind Database
 
Connection String
Once database is downloaded you can attach the same to your SQL Server Instance. I am making use of SQL Server 2005 Express hence below is my Connection string.
<connectionStrings>
      <addname="constr"connectionString="Data Source = .\SQLExpress;
       Initial Catalog = Northwind; Integrated Security = true"/>
</connectionStrings>


AJAX Control Toolkit
Download the AJAX Control Toolkit DLL using the link below and add reference to your project.
Download AJAX Control Tookit
 
Once that’s done register it on the ASPX page as given below
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 
HTML Markup
Below is the HTML markup of the ASP.Net Web Page
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
</asp:ScriptManager>
 
<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
 
As you can notice above I have placed a ScriptManager, a TextBox that will act as AutoComplete and the AJAX Control Toolkit AutoCompleteExtender. Also I am calling a method SearchCustomers using the ServiceMethod property
 
Server Side Methods
The following method is used to populate the Auto complete list of customers
C#
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select ContactName from Customers where " +
            "ContactName like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["ContactName"].ToString());
                }
            }
            conn.Close();
            return customers;
        }
    }
}
 
VB.Net
<System.Web.Script.Services.ScriptMethod(), _
System.Web.Services.WebMethod()> _
Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
    Dim conn As SqlConnection = New SqlConnection
    conn.ConnectionString = ConfigurationManager _
         .ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand
    cmd.CommandText = "select ContactName from Customers where" & _
        " ContactName like @SearchText + '%'"
    cmd.Parameters.AddWithValue("@SearchText", prefixText)
    cmd.Connection = conn
    conn.Open()
    Dim customers As List(Of String) = New List(Of String)
    Dim sdr As SqlDataReader = cmd.ExecuteReader
    While sdr.Read
            customers.Add(sdr("ContactName").ToString)
    End While
    conn.Close()
    Return customers
End Function
 
The above method simply searches the customers table and returns the list of customer names that matches the prefix text.
Note: It is very important that you keep the signature of the method same as what given above. The method must have two parameters namely
1. prefixText (string)
2. count (int)
Otherwise the method won’t work with AutoCompleteExtender.
 
ASP.Net AJAX AutoCompleteExtender without using WebServices

That’s it in this article. You can download the source code in C# and VB.Net using the link below
AutoCompleteExtenderWithoutWebService.zip

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

mohd said:
hi great articlebr how we can caching Listcustomers in this example
February 04, 2010  

Bhanu said:
Hi. Thanks for the tutorial. It is very helpful.br I found it to be slightly slow it takes some time for the auto complete to fill up the things.
February 05, 2010  

Mudassar Khan said:
Reply To: Bhanu
Hi,
The code in this article does not do any change with normal working of control. Thus the speed totally depends on the control and your server

There's another alternative using jQuery
http://www.aspsnippets.com/Articles/Using-jQuery-AutoComplete-Plugin-in-ASP.Net.aspx
February 05, 2010  

veeru said:
it is good for it is not working 2.0 tell me
February 11, 2010  

Mudassar Khan said:
Reply To: veeru
Hi Veeru,

The toolkit version used for this article works only with ASP.Net 3.5

Hence you need to download an older version if you need to make it work with 2.0
February 11, 2010  

BKali said:
Thank you so much.. i was roaming around all the blogs available but nothing worked perfect as yours.br br Thanks much
February 16, 2010  

Shantanu said:
Thanks a lot.br Six months back I googled for this but couldnt find one.br Now within few minuts Im done.br br BTW what is the use of count in the method SearchCustomers(string prefixText int count) br As I see no further usage of it.br
February 22, 2010  

Mudassar Khan said:
Reply To: Shantanu
Yes there's no use but still you have to keep it otherwise it won't work
February 22, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code