Ajax AutoComplete Extender无法正常工作。需要帮忙 :( :( [英] Ajax AutoComplete Extender not working. Need help :( :(
本文介绍了Ajax AutoComplete Extender无法正常工作。需要帮忙 :( :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过后面的代码获取ajax自动完成扩展程序,并尝试匹配数据库中的模式。但它不起作用。请告诉我我犯的是什么错误..谢谢.. :)
I'm trying to get ajax autocomplete extender work through the code behind, and trying to match the pattern from a database. but it doesn't work. Kindly please tell me what mistake I'm making.. Thank you.. :)
<ajaxToolkit:AutoCompleteExtender ServiceMethod="SearchCustomers"
MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="TextBox_Search"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</ajaxToolkit:AutoCompleteExtender>
<asp:TextBox ID="TextBox_Search" runat="server" Height="28px" Width="206px"></asp:TextBox>
public partial class MasterPage_UserProfile : System.Web.UI.MasterPage
{
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Ebazzar"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select pname from products where " +"pname like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> Products = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Products.Add(sdr["pname"].ToString());
}
}
conn.Close();
return Products;
}
}
}
}
推荐答案
//return type should be a array
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static string[] SearchCustomers(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Ebazzar"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
//create parameter like below
cmd.CommandText = "select pname from products where pname like @SearchText";
//set the parameter values as below
cmd.Parameters.AddWithValue("@SearchText", prefixText + "%");
cmd.Connection = conn;
conn.Open();
List<string> Products = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Products.Add(sdr["pname"].ToString());
}
}
conn.Close();
return Products.ToArray();
}
}
}
这篇关于Ajax AutoComplete Extender无法正常工作。需要帮忙 :( :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文