Monday, 27 February 2012

Ajax AutoCompleteExtender using webservice,linq

DESIGN THE PAGE LIKE THIS :
<form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <center>
    <asp:TextBox ID="tb" runat="server"></asp:TextBox><asp:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="tb" CompletionSetCount="12" CompletionInterval="1" MinimumPrefixLength="2" ServicePath="~/WebService.asmx" ServiceMethod="x"
        runat="server">
    </asp:AutoCompleteExtender>
    </center>
    </div>
    </form>
WEBSERVICE CODE :

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    DataClassesDataContext o = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());
    int i;
    [WebMethod]
   public string[] x(string prefixText)
    {
        i = 0;
        var m = from y in o.EMPs where y.NAME.StartsWith(prefixText) select y;
        string[] s = new string[m.Count()];
        foreach (var n in m)
        {
            s[i] = n.NAME;
            i++;
        }
        return s;
     
    }
}

No comments:

Post a Comment