Asp.net AutoCompleteExtender不工作,也没有启动page方法,而是在响应中显示垃圾页面标记

Asp.net AutoCompleteExtender不工作,也没有启动page方法,而是在响应中显示垃圾页面标记,asp.net,ajax,asp.net-ajax,ajaxcontroltoolkit,Asp.net,Ajax,Asp.net Ajax,Ajaxcontroltoolkit,我正在尝试使用page方法将Ajax Autocomplete extender方法放在我的子页面中。不知何故,page方法没有启动,相反,page保持了一秒钟,表示正在执行长脚本,并在目标文本框中显示一些随机的页面标记 我尝试设置上下文键参数,但没有成功。我甚至在extender的sevicepath属性中设置了代码隐藏文件路径,但这也不起作用 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEvent

我正在尝试使用page方法将Ajax Autocomplete extender方法放在我的子页面中。不知何故,page方法没有启动,相反,page保持了一秒钟,表示正在执行长脚本,并在目标文本框中显示一些随机的页面标记

我尝试设置上下文键参数,但没有成功。我甚至在extender的sevicepath属性中设置了代码隐藏文件路径,但这也不起作用

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="FleetBooking.aspx.cs" Inherits="TMSAdmin.FleetBooking" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
    TagPrefix="CC1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <head runat="server"></head>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
    </asp:ScriptManager>

    <section class="content-header">
        <h1>Fleet booking
            <small>Preview page</small>
        </h1>
    </section>
    <div class="row">
        <div class="col-md-2">
            <h4>Route Name:</h4>
        </div>
        <div class="col-md-2">
            <asp:TextBox ID="txtRoutes" runat="server" CssClass="form-control" />
            <CC1:AutoCompleteExtender ServiceMethod="GetRoutes" MinimumPrefixLength="1" 
                CompletionInterval="100"
                EnableCaching="false" CompletionSetCount="10"
                TargetControlID="txtRoutes" 
                ID="AutoCompleteExtender2"
                runat="server" FirstRowSelected="false"
                CompletionListCssClass="autocomplete_completionListElement"
                CompletionListItemCssClass="autocomplete_listItem"
                CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
            </CC1:AutoCompleteExtender>
        </div>
</asp:Content>

车队预订
预览页
路由名称:
代码隐藏:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> GetRoutes(string prefixText, int count)
{
    using (SqlConnection con = new SqlConnection())
    {
        //con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        con.ConnectionString = ClsCon.myconn;
        using (SqlCommand com = new SqlCommand())
        {
            com.CommandText = "select Route_Name from tbl_RouteMaster where "
               + "Route_Name like ' @Search + '%'";

            com.Parameters.AddWithValue("@Search", prefixText);
            com.Connection = con;
            con.Open();
            List<string> routeNames = new List<string>();
            using (SqlDataReader sdr = com.ExecuteReader())
            {
                while (sdr.Read())
                {
                    routeNames.Add(sdr["Route_Name"].ToString());
                }
            }
            con.Close();
            return routeNames;
        }
    }
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
公共静态列表GetRoutes(字符串前缀、整数计数)
{
使用(SqlConnection con=newsqlconnection())
{
//con.ConnectionString=ConfigurationManager.ConnectionString[“Conn”].ConnectionString;
con.ConnectionString=ClsCon.myconn;
使用(SqlCommand com=newsqlcommand())
{
com.CommandText=“从tbl\U RouteMaster where中选择路由\u名称”
+“路由名称,如'@Search+'%'”;
com.Parameters.AddWithValue(“@Search”,prefixText);
com.Connection=con;
con.Open();
List routeNames=新列表();
使用(SqlDataReader sdr=com.ExecuteReader())
{
while(sdr.Read())
{
添加(sdr[“Route_Name”].ToString());
}
}
con.Close();
返回路由名称;
}
}
}

我希望它会触发我的page方法,这样我就可以调试并解决任何问题。

我认为SQL中有一个错误:

+ "Route_Name like ' @Search + '%'";    // missing double-quote.
应该是

+ "Route_Name like '" + @Search + "%'";


讨论了一个类似的问题,但我无法使建议的解决方案起作用

我能够使用web服务实现另一种方法。在WebService1.asmx中添加以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebFormsProject
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public List<string> GetRoutes(string prefixText)
        {
            var list = new List<string>();
            list.Add("Item 1");
            list.Add("Item 2");

            return list;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
命名空间WebFormsProject
{
/// 
///WebService 1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类WebService 1:System.Web.Services.WebService
{
[网络方法]
公共列表GetRoutes(字符串前缀)
{
var list=新列表();
列表。添加(“第1项”);
列表。添加(“第2项”);
退货清单;
}
}
}
按如下方式更新AutoCompleteXtender:

<CC1:AutoCompleteExtender 
    ServiceMethod="GetRoutes" ServicePath="~/WebService1.asmx"
    MinimumPrefixLength="1" 
    CompletionInterval="100"
    EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtRoutes" 
    ID="AutoCompleteExtender2"
    runat="server" FirstRowSelected="false"
    CompletionListCssClass="autocomplete_completionListElement"
    CompletionListItemCssClass="autocomplete_listItem"
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</CC1:AutoCompleteExtender>

使用上面的代码,我可以在文本框中显示结果


希望这有帮助。

不,这是我传递的SQL参数,它不是c变量。啊,当然。尝试从原始文件中删除参数前的第一个单引号。(不是肯定的…)您可能需要用一个根本没有被调用的
+
GetRoutes
来替换它?如果它不起作用,我将首先删除扩展器和页面顶部的
Register
指令,然后重新添加扩展器,以确保它起作用。还要确保ACToolkit dll位于存储箱中。作为起点。它是普通的asp.net应用程序还是任何web服务?尝试删除[System.Web.Services.WebMethod][System.Web.Script.Services.ScriptMethod]是的,GetRoutes根本没有被命中,它是一个asp.net应用程序。我已经添加了[System.Web.Services.WebMethod]以便ajax AutoExtender工作。不确定它是否相关,但尝试删除
元素。
<CC1:AutoCompleteExtender 
    ServiceMethod="GetRoutes" ServicePath="~/WebService1.asmx"
    MinimumPrefixLength="1" 
    CompletionInterval="100"
    EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtRoutes" 
    ID="AutoCompleteExtender2"
    runat="server" FirstRowSelected="false"
    CompletionListCssClass="autocomplete_completionListElement"
    CompletionListItemCssClass="autocomplete_listItem"
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</CC1:AutoCompleteExtender>