首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动完成扩展程序不工作,也不触发页面方法,而是在响应中显示垃圾页面标记

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

Stack Overflow用户
提问于 2019-07-17 03:05:38
回答 2查看 409关注 0票数 2

我试着把Ajax自动完成扩展器方法放在我的子页面中,使用的页面method.Somehow页面方法没有启动,相反,页面保持了一秒钟,说长脚本正在执行,并在目标文本框中显示一些随机的页面标记。

我尝试设置context key参数,但不起作用。我甚至在扩展程序的sevicepath属性中设置了文件路径后面的代码,但这也不起作用

代码语言:javascript
复制
<%@ 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>

代码隐藏:

代码语言:javascript
复制
[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;
        }
    }
}

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

EN

回答 2

Stack Overflow用户

发布于 2019-07-27 02:37:24

here也讨论过类似的问题,但我无法使提出的解决方案起作用。

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

使用上面的代码,我可以在文本框中得到结果。

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2019-07-17 11:33:18

我认为在SQL中有一个错误:

代码语言:javascript
复制
+ "Route_Name like ' @Search + '%'";    // missing double-quote.

应该是

代码语言:javascript
复制
+ "Route_Name like '" + @Search + "%'";

代码语言:javascript
复制
+ "Route_Name like '%" + @Search + "%'";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57063904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档