【问题标题】:Ajax autocomplete extender not working in masterpageAjax 自动完成扩展器在母版页中不起作用
【发布时间】:2012-10-05 07:00:42
【问题描述】:

我正在尝试从数据库创建搜索。当用户开始在文本框中输入时,带有自动完成扩展器的文本框下方会出现一个城镇列表。它在普通网页中工作正常,但如果我将代码放在母版页中,它就无法工作。 “GetList”不是事件触发。有什么建议?谢谢。

母版页代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Master.master.cs" Inherits="Obelo.MasterPages.Master1" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body bgcolor="#5c5b5b">
    <form id="form1" runat="server">
         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
         <asp:textbox id="tbFindWhat" runat="server" Width="210px" Font-Names="Arial" 
               Font-Size="9pt" ForeColor="#FF6600" BackColor="#1E1E1E" 
               BorderColor="White" BorderStyle="Solid" BorderWidth="1px" 
               Style="padding:0 0 0 10px; margin: -2px"></asp:textbox>
         <asp:AutoCompleteExtender ID="AutoCompleteExtender1" 
               runat="server" TargetControlID="tbFindWhat" MinimumPrefixLength="1" 
               EnableCaching="true" CompletionSetCount="1" 
               CompletionInterval="1000" ServiceMethod="GetList">
         </asp:AutoCompleteExtender>
         <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
         </asp:ContentPlaceHolder>
     </form>
</body>
</html>

后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace MasterPage
{
    public partial class Master1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetList(string prefixText)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BusinessConnectionString"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tblTowns where Name like @Name+'%'", con);
            cmd.Parameters.AddWithValue("@Name", prefixText);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<string> CountyNames = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                CountyNames.Add(dt.Rows[i][1].ToString());
            }
            con.Close();
            return CountyNames;
        }
    }
}

【问题讨论】:

  • 用户控件中是否包含扩展程序所需的所有脚本?有没有被浏览器捕捉到的 JS 错误?
  • 嗨,一切都和普通的 asp.net 页面一样,它工作正常,没有使用 javascript。

标签: c# asp.net ajax autocomplete


【解决方案1】:

根据文档,服务方法的签名必须是:

 public string[] GetCompletionList(string prefixText, int count, string contextKey) 
 { 
    ... 
 }

结帐自动完成扩展属性here

【讨论】:

  • count 和 contextkey 有什么改变吗?因为在我把它放在母版页之前没问题。
  • @user1726528 不,我是说返回类型!
【解决方案2】:

我搜索了很多,但是如果您将 webmethod 放在每个页面上,它就可以工作,这是不好的修复,但它会使其工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2013-11-06
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多