我试着把Ajax自动完成扩展器方法放在我的子页面中,使用的页面method.Somehow页面方法没有启动,相反,页面保持了一秒钟,说长脚本正在执行,并在目标文本框中显示一些随机的页面标记。
我尝试设置context key参数,但不起作用。我甚至在扩展程序的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;
}
}
}我希望它会触发我的页面方法,这样我就可以调试和解决任何问题。
发布于 2019-07-27 02:37:24
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>使用上面的代码,我可以在文本框中得到结果。
希望这能有所帮助。
发布于 2019-07-17 11:33:18
我认为在SQL中有一个错误:
+ "Route_Name like ' @Search + '%'"; // missing double-quote.应该是
+ "Route_Name like '" + @Search + "%'";或
+ "Route_Name like '%" + @Search + "%'";https://stackoverflow.com/questions/57063904
复制相似问题