Thursday, June 28, 2012

AJAX autocompleteExtender Textbox using web service

In this post i will show how to assign the data to autocomplete textbox from web service in this i have used a webservice "FirstAutoComplete.asmx",which has one method GetdeptName() is used to get the data from database based user input.
The ajax tool kit provides a control to perform this kind things./i:e autocomplete extender.It has one property to acces the web servoce which is"ServicePath"
<form id="test" runat="ServeR">
<asp:ScriptManager ID="sm" runat="Server">
<asp:TextBox od="txttest" runat="Server"></asp:TextBox>
<Ajaxtest:AutoCompleteExtender ID="ac" runat="Server" TargetControlID="txttest" Servicemethod="GetdeptName" ServicePath="FirstAutoComplete.asmx "
MinimumPrefixLenth="1" EnableCaching="true" CompletionSetCount="12">
Web service:
Here the web method class has placed app_codefolder.
public class FirstAutoComplete:System.Web.Services.Webservice
public string[] GetdeptName(string inputText)
        string sqlstr = "select MAINCODE, MAINNAME from MstMainMaster where maincode like 'ddp%' and  MAINNAME like @inputText";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, ConfigurationManager.ConnectionStrings["Con"].ToString());
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value  ="%"+ inputText + "%";
        DataTable dt = new DataTable();
        string[] outputitems = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
            outputitems.SetValue(dr["MAINNAME"].ToString(), i);
        return outputitems;


Post a Comment