Monday, 20 June 2016

Auto Complete Text box using Ajax Auto Complete Extender and Retrieve the Database Column Values:

Step 1 : In your code behind page  add ajax register tag like below. 

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

          

Step 2 :Add ajax tool kit reference both Solution Explorer and Tool Box in your project.

Step 3: Add the text box in your page.


 <asp:TextBox ID="txtdepname" runat="server" Width="175px" Height="25px"></asp:TextBox>

                <asp:AutoCompleteExtender ID="txtdepname_AutoCompleteExtender" runat="server" MinimumPrefixLength="1" CompletionSetCount="10"

                    DelimiterCharacters="" Enabled="True" ServiceMethod="GetCity" TargetControlID="txtdepname" ServicePath="">

                </asp:AutoCompleteExtender>



step 4: You need to database and table with values .

step 5: C# page contains the code below.


[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetCity(string prefixText, int count)
    {

        MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["constr1"].ConnectionString);
        MySqlCommand cmdsur = new MySqlCommand("select distinct empname from empwork where " + "empname like '" + prefixText + "%'", con);
        cmdsur.Connection = con;
        con.Open();
        List<string> name = new List<string>();
        MySqlDataReader sdr = cmdsur.ExecuteReader();

        while (sdr.Read())
        {
            name.Add(sdr["empname"].ToString());
        }
        sdr.Close();
        con.Close();
        return name;
    }


Step 6: Output

No comments:

Post a Comment

How to create a simple Hello World website in ASP.NET MVC using Razor Syntax: (Note: I am using Visual Studio 2012 ) Step 1: ...