自动完成功能未触发
本文关键字:功能 成功 | 更新日期: 2023-09-27 17:49:23
我有一个使用ASP的项目。NET 4.5和c#。我有一个文本框服务器控件,绑定到一个asp AutoCompleteExtender。问题是,它似乎不是连接到数据库有一个bug在我的代码的某个地方。为此,我没有使用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;
}
}
}
}
引起我注意的问题是:您在自动完成扩展程序中调用错误的方法
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
public static List<string> SearchStudents(string prefixText, int count)
{
在这里你可以看到你已经传递SearchCustomers作为ServiceMehtod,但在你的页面你使用SearchStudents。
最近我尝试了twitter的TypeAhead自动完成插件,这是简单和快速的自动完成扩展器相比。看看你可能会喜欢:在asp .net中使用twitter提前输入自动完成文本框
[我想你是用这个链接作为参考的,但它对我有用][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>
网络。配置
<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>
And Don't forget to create table And insert some record in SQL Server