Ajax自动完成扩展程序无法在母版页中使用

时间:2012-10-15 10:02:46

标签: c# asp.net ajax autocomplete

我正在尝试从数据库创建搜索。当用户开始在文本框中键入内容时,文本框下会显示一个城镇列表,其中包含autocompleteextender。它在普通网页中工作正常,但如果我将代码放在母版页中则无法正常工作。 “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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

结帐自动填充扩展程序属性here

答案 1 :(得分:0)

我搜索了很多但是如果你在每个页面上放置webmethod它会有效,这是很糟糕的修复,但它会使它工作