SUN
SUN

Reputation: 973

Asp.net autocomplete Extender not working

I want to make textbox autocomplete with database. I used following code but in output extender shows html codes. It is not even executing code behind functions. Following code I have used which is not working properly.

enter image description here

ASPx page

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CS.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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>

<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" 
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>

C# Code

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (MySqlConnection conn = new MySqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["conio"].ConnectionString;
        using (MySqlCommand cmd = new MySqlCommand())
        {
            cmd.CommandText = "select clientID from clientsDetails where "  +
            "clientID like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers = new List<string>();
            using (MySqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["clientID"].ToString());
                }
            }
            conn.Close();
            return customers;
        }
    }
}

Upvotes: 2

Views: 3502

Answers (2)

deepika
deepika

Reputation: 1

    add  <cc1:ToolkitScriptManager></cc1:ToolkitScriptManager> instead of   <asp:ScriptManager></asp:ScriptManager>

your aspx page  ----

    <cc1:ToolkitScriptManager ID="ScriptManager1" runat="server" 
    EnablePageMethods = "true">
    </cc1:ToolkitScriptManager>

    <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
               <cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="1" CompletionInterval="10"  
                EnableCaching="false" CompletionSetCount="10" TargetControlID="txtContactsSearch" ID="AutoCompleteExtender2"  
                runat="server" FirstRowSelected="false"></cc1:AutoCompleteExtender>  


    your cs page----

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

                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager
                            .ConnectionStrings["conio"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "select clientID from clientsDetails where "+"clientID like @SearchText + '%'";
                        cmd.Parameters.AddWithValue("@SearchText", prefixText);
                        cmd.Connection = conn;
                        conn.Open();
                        List<string> customers = new List<string>();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                customers.Add(sdr["name"].ToString());
                            }
                        }
                        conn.Close();
                        return customers;
                    }
                }
            }

Upvotes: 0

user1429080
user1429080

Reputation: 9166

You are viewing the page that contains the TextBox on the URL:

http://localhost:64890/CS

This seems to be mapped by a route to the page

CS.aspx

When the AutoCompleteExtender sends it's AJAX request, it will send it to this address:

http://localhost:64890/CS/SearchCustomers

This is likely picked up by some route that you have set up, and the result is that it does not go to the PageMethod in CS.aspx.cs but is instead handled in some other way.

Assuming your CS.aspx page in in the web application root folder, you may be able to fix it by adding this:

ServicePath="~/CS.aspx"

to you AutoCompleteExtender. Like so:

<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
    MinimumPrefixLength="2"
    ServicePath="~/CS.aspx"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</cc1:AutoCompleteExtender>

Upvotes: 1

Related Questions