AJAX自动完成扩展器与Web服务
本文关键字:Web 服务 扩展器 AJAX | 更新日期: 2023-09-27 18:19:33
我对C Sharp非常陌生。在我的项目中,我正在设计一个搜索页面,其中我有带有AJAX控件自动扩展程序的文本框,当用户在文本框中键入单词时,我正在使用Web服务来填充文本框(这是我第一次使用Web服务,我不知道将使用什么Web服务)。我已经正确地指定了所有内容,当我运行程序并键入Word时,没有任何响应。
这个问题可能看起来像是一个重复的问题,但不是。我已经在谷歌上搜索了很多次,看到很多博客都用上面显示的例子解决了,但没有结果。有人帮帮我,
我的Web服务代码是,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using SubSonic;
using DataAccessLayer;
using System.Web.Configuration;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Search : System.Web.Services.WebService
{
public void Autocomplete()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
if (count == 0)
{
count = 10;
}
DataTable dt = GetRecords(prefixText);
List<string> items = new List<string>(count);
for (int i = 0; i < dt.Rows.Count; i++)
{
string strName = dt.Rows[i][0].ToString();
items.Add(strName);
}
return items.ToArray();
}
public DataTable GetRecords(string strName)
{
string QueryString;
QueryString = System.Configuration.ConfigurationManager.ConnectionStrings ["IUMSNXG"].ToString();
using (SqlConnection obj_SqlConnection = new SqlConnection(QueryString))
{
using (SqlCommand obj_Sqlcommand = new SqlCommand())
{
obj_Sqlcommand.CommandType = CommandType.StoredProcedure;
obj_Sqlcommand.CommandText = "LRS_SP_CBFM_Sel";
obj_Sqlcommand.Connection = obj_SqlConnection;
obj_SqlConnection.Open();
obj_Sqlcommand.Parameters.AddWithValue("@animalCode", strName);
SqlDataAdapter dt = new SqlDataAdapter(obj_Sqlcommand);
DataSet ds=new DataSet();
dt.Fill(ds);
obj_SqlConnection.Close();
return ds.Tables[0];
}
}
}
}
我的AJAX工具脚本管理器是
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Search.asmx" />
</Services>
</asp:ToolkitScriptManager>
我的文本框和自动完成扩展器是,
<Anthem:TextBox ID="srchtxt" runat="server" AutoUpdateAfterCallBack="true"
Height="19px" Width="200px"></Anthem:TextBox>
<asp:AutoCompleteExtender ID="srchtxt_AutoCompleteExtender" runat="server"
CompletionInterval="100" DelimiterCharacters="" Enabled="True"
ServicePath="~/Search.asmx"
TargetControlID="srchtxt" UseContextKey="True"
ServiceMethod="GetCompletionList">
</asp:AutoCompleteExtender>
之所以会发生这种情况,是因为在GetCompletionList(string prefixText, int count)
的代码隐藏文件中,您使用了两个参数,而在.aspx页面中,您调用这个没有参数的方法,就像简单的ServiceMethod="GetCompletionList"
一样。如果您从GetCompletionList()
中删除该参数,您的代码就会工作。
我知道你需要参数string prefixText
。这个变量prefixText
只是在文本框中输入的值,用于从数据库中获取数据,这样它就会相应地为您提供数据。因此,不在查询中使用此参数值,只需使用类似select * from table where name like '%textbox.Text%'
的文本框值