Tuesday, July 1, 2014

Jquery Ajax autocomplete for ASP.NET

In this example I show you can create an autocomplete in  jquery which makes ajax call  to code behind web method.

I use couple of things:

1. JSON.net
2. jquery-ui
3. json.js

Here is the AspAjaxDev.aspx page used :



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AspAjaxDev.aspx.cs" Inherits="TestWebApp.AspAjaxDev" %>






    Ajax works
    
    
    
    
    
    

    



    
Name:
Chosen Value:


The codebehind web method to which calls were made:-


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetNames(string p)
        {
            Dictionary names = new Dictionary();

            names.Add(1, "Keerthi");
            names.Add(2, "Keeith");
            names.Add(3, "Kay");
            names.Add(4, "Keny");
            names.Add(5, "Ness");
            names.Add(6, "May");
            names.Add(7, "Ferro");
            string json = JsonConvert.SerializeObject(names.Where(x => x.Value.StartsWith(p, true, CultureInfo.CurrentCulture)).ToDictionary(x -> x.Key, x -> x.Value), Formatting.None);
            return json;
}
       

No comments:

Post a Comment