Asp.net autocomplete Extender not working

♀尐吖头ヾ 提交于 2019-12-24 01:23:46

问题


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.

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

回答1:


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>



回答2:


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


来源:https://stackoverflow.com/questions/41462113/asp-net-autocomplete-extender-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!