Ajax AutocompleteExtender 未从 SQL Server 获取数据?
我正在尝试使用 Ajax AutoCompleteExtender 基于数据库创建一个具有自动完成功能的搜索字段,当我运行代码时,它只获取搜索列表中显示的奇怪字母。
在 aspx.cs 文件中:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> SearchEmails(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["iasConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select email from Emails where email like @email + '%'";
cmd.Parameters.AddWithValue("@email", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> emails = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
emails.Add(sdr["email"].ToString());
}
}
conn.Close();
return emails;
}
}
}
在 aspx 文件(HTML)中:
<head runat="server">
<title>Emails</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="EmailSearch" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="SearchEmails"
MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="EmailSearch" FirstRowSelected="false">
</ajax:AutoCompleteExtender>
</div>
</form>
</body>
I'm trying to create a search field with autocomplete based on the database using Ajax AutoCompleteExtender, and when I run the code it fetch nothing but a weird letters shows in the search list.
In aspx.cs file:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> SearchEmails(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["iasConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select email from Emails where email like @email + '%'";
cmd.Parameters.AddWithValue("@email", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> emails = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
emails.Add(sdr["email"].ToString());
}
}
conn.Close();
return emails;
}
}
}
In aspx file(HTML):
<head runat="server">
<title>Emails</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="EmailSearch" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="SearchEmails"
MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="EmailSearch" FirstRowSelected="false">
</ajax:AutoCompleteExtender>
</div>
</form>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,应该可以。这对我有用:
您可能只需将该函数标记为网络方法即可。
这对我有用:
我强键入了参数,但尝试将您的代码作为 WebMethod,看看是否有效。
我的标记:
输出:
Hum, that should work. This works for me:
It might be that you have to JUST tag the function as a webmethod.
This works for me:
I strong typed the parameter, but try your code as WebMethod, and see if that works.
My Markup:
Output: