Click here to Skip to main content
Click here to Skip to main content

ASP.NET Ajax AutoCompleteExtender Without Using Web Service

By , 28 Jun 2012
 

Introduction

This tip shows how to use Ajax AutoCompleteExtender in an ASP.NET application, without adding a Web Service.

Steps

  • Create an ASP.NET 4.0 Web Application
  • Add reference to AjaxControlToolit
  • Open Default.aspx
  • Add Script Manager
  • Add Ajax AutoCompleteExtender 

Using the Code

Using the code (default.aspx)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:ScriptManager ID="ScriptManager1" runat="server" 
EnablePageMethods = "true">
</asp:ScriptManager>

Enter a Product Name
<asp:TextBox ID="txtProductList" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="GetProductList" 
    MinimumPrefixLength="1"
    CompletionInterval="0" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="txtProductList"
    ID="autoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static string[] GetProductList(string prefixText, int count)
    {
        // Get the Products From Data Source. Change this method to use Database
        List<string> productList = GetProducts();

        // Find All Matching Products
        var list = from p in productList
                   where p.Contains(prefixText)
                   select p;

        //Convert to Array as We need to return Array
        string[] prefixTextArray = list.ToArray<string>();

        //Return Selected Products
        return prefixTextArray;
    }  

Points of Interest

  • If you want that your method responds to the Scripting Language, make sure to add [System.Web.Script.Services.ScriptMethod()] to method attribute. Else, this won't work
  • And yes, this does not require addition of Web Service!

History

  • 29th June, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SmartCodar
India India
Member
I am a Tech Lead / .Net Architect,
 
I love working with Business Challenges, Requirements
 
I work mainly for Designing eCommerce Portals Using ASP.Net enabled Frameworks such as Mediachase, zNode and Storefront.

I love writing my Articles at Code Project and my site www.SmartCodar.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4membermusthaan19 Mar '13 - 3:13 
Hi
Thanks,
 
How ti align it ?
QuestionGood ArticlememberAlireza_136222 Dec '12 - 0:46 
Thanks
GeneralReally not a web service?memberPankaj Nikam5 Jul '12 - 22:04 
According to MSDN [^], Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. It is indirectly a WebService.
 
Anyway, great article short and simple.
Always Keep Smiling.
Yours Pankaj Nikam

GeneralRe: Really not a web service? [modified]memberSmartCodar6 Jul '12 - 23:49 
Yes, Can you Create a XML Consumer of this Web Method in Other Pages? Or Projects? Like we do for typical Web Service.
 
[Web Method] with Script Tag : This Method can be exposed to Scripting Language, Produces XML
 
[Web Service] : Which provides WSDL / And Can we communicated with SOAP
 
A typical Web Service has lot of features, one of the required element is [Web Method]
 
ASPX Page is calling Static [Web Method] from Code Behind and not Web Service. I think we cannot call this Web Method from Other Projects...Needs Schema or WSDL Smile | :)
 
Why I Did This : One of ASP.Net Project, Web Service was as a Part of ASP.Net App. This is a Potential Threat as any one can access. So did this Trick. Smile | :)
 
I am always open to Upgrade myself. Ideas are always Welcome.
 
Thanks a Lot

modified 7 Jul '12 - 5:56.

GeneralMy vote of 5memberKamil Zmeskal29 Jun '12 - 4:14 
Simple and easy!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 29 Jun 2012
Article Copyright 2012 by SmartCodar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid