自动完成扩展程序不起作用并且不触发页面方法,而是在响应中显示垃圾页面标记

时间:2019-07-16 19:05:37

标签: asp.net ajax asp.net-ajax ajaxcontroltoolkit

我试图使用页面方法在子页面中放置Ajax自动完成扩展器方法。页面方法不触发,而是页面保持一秒钟,说长脚本正在执行,并在目标文本框中显示了一些随机页面标记。

我尝试设置上下文键参数,但是没有用。我什至将我的代码设置在扩展程序的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;
        }
    }
}

我希望它会触发我的page方法,以便我可以调试和解决问题(如果有)。

2 个答案:

答案 0 :(得分:0)

我认为SQL中有一个错误:

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

应该是

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

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

答案 1 :(得分:0)

讨论了类似的问题here,但我无法使建议的解决方案生效。

我能够使用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;
        }
    }
}

按以下方式更新AutoCompleteExtender:

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

使用上面的代码,我能够在文本框中获得结果。

希望这会有所帮助。