[英]Ajax autocomplete extender not working in masterpage
我正在尝试从数据库创建搜索。 当用户开始在文本框中输入内容时,带有自动完成扩展功能的城镇列表将显示在文本框下方。 它在正常的网页上工作正常,但是如果我将代码放在母版页中则无法正常工作。 “ 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;
}
}
}
根据文档,服务方法的签名必须为:
public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
...
}
在此处签出AutoComplete扩展器属性。
我搜索了很多内容,但是如果您将webmethod放在每个页面上,它都可以解决,但是它会使其工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.