Ajax AutocompleteExtender 未从 SQL Server 获取数据?

发布于 2025-01-11 00:13:33 字数 2011 浏览 1 评论 0原文

我正在尝试使用 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>

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2025-01-18 00:13:33

嗯,应该可以。这对我有用:

您可能只需将该函数标记为网络方法即可。

这对我有用:

   [WebMethod]
    public static List<string> SearchHotels(string prefixText, int count)
    {            
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmd = new SqlCommand("",conn))
            {
                cmd.CommandText = "SELECT HotelName from tblHotels where HotelName like @h + '%' " +
                   "ORDER BY HotelName";

                cmd.Parameters.Add("@h", SqlDbType.NVarChar).Value = prefixText;
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmd.ExecuteReader());

                List<string> Hotels = new List<string>();
                foreach (DataRow OneRow in rstData.Rows)
                    Hotels.Add(OneRow["HotelName"].ToString());

                return Hotels;
            }
        }
    }

我强键入了参数,但尝试将您的代码作为 WebMethod,看看是否有效。

我的标记:

<asp:TextBox ID="TextBox1" runat="server" Width="255px"></asp:TextBox>

<ajaxToolkit:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" 
    runat="server" BehaviorID="TextBox1_AutoCompleteExtender" 
    DelimiterCharacters=""  TargetControlID="TextBox1" MinimumPrefixLength="1" 
    ServiceMethod="SearchHotels" CompletionInterval="500" EnableCaching="False">
</ajaxToolkit:AutoCompleteExtender>

输出:

在此处输入图像描述

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:

   [WebMethod]
    public static List<string> SearchHotels(string prefixText, int count)
    {            
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmd = new SqlCommand("",conn))
            {
                cmd.CommandText = "SELECT HotelName from tblHotels where HotelName like @h + '%' " +
                   "ORDER BY HotelName";

                cmd.Parameters.Add("@h", SqlDbType.NVarChar).Value = prefixText;
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmd.ExecuteReader());

                List<string> Hotels = new List<string>();
                foreach (DataRow OneRow in rstData.Rows)
                    Hotels.Add(OneRow["HotelName"].ToString());

                return Hotels;
            }
        }
    }

I strong typed the parameter, but try your code as WebMethod, and see if that works.

My Markup:

<asp:TextBox ID="TextBox1" runat="server" Width="255px"></asp:TextBox>

<ajaxToolkit:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" 
    runat="server" BehaviorID="TextBox1_AutoCompleteExtender" 
    DelimiterCharacters=""  TargetControlID="TextBox1" MinimumPrefixLength="1" 
    ServiceMethod="SearchHotels" CompletionInterval="500" EnableCaching="False">
</ajaxToolkit:AutoCompleteExtender>

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文