2

我正在尝试从数据库创建搜索。当用户开始在文本框中输入时,带有自动完成扩展器的文本框下方会出现一个城镇列表。它在普通网页中工作正常,但如果我将代码放在母版页中,它就无法工作。“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;
        }
    }
}
4

2 回答 2

1

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

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

在此处签出 AutoComplete 扩展程序属性。

于 2012-10-15T10:13:43.313 回答
0

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

于 2013-04-25T20:40:29.883 回答