自动完成功能未触发

时间:2015-05-15 05:39:50

标签: c# asp.net

我有一个使用ASP.NET 4.5和C#的项目。我有一个与asp AutoCompleteExtender绑定的文本框服务器控件。问题是,似乎要么没有连接到数据库,我的代码中有一个错误。为此,我没有使用Web服务只是一个简单的aspx和aspx.cs代码。我还没有能够调试以查明实际问题,因为我得到了

  

通过反射调用的方法引发的未捕获异常

我正在弄清楚造成这个问题的原因。

这是我的文本框控件:

<div class="col-md-10">              
            <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
            <cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
                MinimumPrefixLength="2"
                CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
                TargetControlID="txtContactsSearch"
                ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
            </cc1:AutoCompleteExtender>
            <asp:RequiredFieldValidator runat="server" ControlToValidate="StudentID" CssClass="text-danger" Display="Dynamic" ErrorMessage="The student ID field is required." />
        </div>          

我在aspx页面上声明了AJAX指令:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

这是实现AutoComplete功能的背后代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> SearchStudents(string prefixText, int count)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager
                    .ConnectionStrings["DefaultConnection"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Fname from Student_Registration_Form where " +
                "Fname like @SearchText + '%'";
                cmd.Parameters.AddWithValue("@SearchText", prefixText);
                cmd.Connection = conn;
                conn.Open();
                List<string> students = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        students.Add(sdr["Fname"].ToString());
                    }
                }
                conn.Close();
                return students;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

引起我注意的问题是:您在自动填充扩展程序中调用了错误的方法

<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"

public static List<string> SearchStudents(string prefixText, int count)
    {

在这里您可以看到您已将SearchCustomers作为ServiceMehtod传递,但在您的页面中您使用的是SearchStudents。

最近我尝试了Twitter的TypeAhead自动完成插件,与Autocomplete Extender相比,它简单快捷。看看你可能会喜欢: 的 Textbox autocomplete using twitter typeahead in asp .net

答案 1 :(得分:0)

[我认为您已使用此链接供您参考。但它对我有用。] [1]

       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Web;
       using System.Web.UI;
       using System.Web.UI.WebControls;
       using System.Configuration;
       using System.Data.SqlClient;
       using System.Web.UI;
       using System.Web.UI.WebControls;
       using System.Collections;

      namespace AutoComplete
       {
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetCompletionList(string prefixText, int count)
    {

        using (SqlConnection conn = new SqlConnection())
        {
            try
            {
                conn.ConnectionString = ConfigurationManager
                        .ConnectionStrings["Default"].ConnectionString;

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select LoginName from Users where " +
                    "LoginName like @SearchText + '%'";
                    cmd.Parameters.AddWithValue("@SearchText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> students = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            students.Add(sdr["LoginName"].ToString());
                        }
                    }
                    conn.Close();
                    return students;
                }
            }
            catch (Exception ex)
            {
                return null;
            }

        }

    }
}

}

ASPX代码:

  <table style="margin-top: 40px; color: White">
    <tr>
        <td>
            Search County
        </td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
                CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="TextBox1"
                ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
            </asp:AutoCompleteExtender>
        </td>
    </tr>
</table>

的Web.Config

  <connectionStrings>
<add name="Default" providerName="System.Data.SqlClient" connectionString="Data Source=SERVERNAME;database=DBNAME;uid=sa;pwd=123;connection reset=false;connection lifetime=1000000;enlist=true;min pool size=1;max pool size=1000000"/>
  </connectionStrings>

并且不要忘记创建表并在SQL Server中插入一些记录