使用ajax的asp.net 3.5中的自动完成文本框 [英] autocomplete text box in asp.net 3.5 using ajax
问题描述
我想在我的数据库中使用ajax在asp.net 3.5中实现自动完成文本框.我有一个使用ajaxtoolkit.dll的示例代码,但它不起作用,我不知道为什么会不起作用怎么办,我想我在代码中遗漏了一些东西,但是我找不到它.由于我是ajax的新手,所以请帮助我
我的代码如下:
Default.aspx
Hi,
i want to implement the auto complete text box in asp.net 3.5 using ajax with my database.I have a sample code which is using ajaxtoolkit.dll but it is not working ,i dont know what happen why it is not working ,i thought that i have missing some thing in the code but what ican not caught it out.As i am new to ajax .So plz help me
my code as follows:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoCompleteTest.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoCompleteTest.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" />
</div>
</form>
</body>
</html>
[Web服务AutoCompleteTest.asmx]
AutoCompleteTest.cs
[web service AutoCompleteTest.asmx]
AutoCompleteTest.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for AutoCompleteTest
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AutoCompleteTest : System.Web.Services.WebService {
public AutoCompleteTest () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCountriesList(string prefixText)
{
DataSet dtst = new DataSet();
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["AppConnString"]);
string strSql = "SELECT Name FROM Colleges WHERE Name LIKE '" + prefixText + "%' ";
SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
sqlCon.Open();
SqlDataAdapter sqlAdpt = new SqlDataAdapter();
sqlAdpt.SelectCommand = sqlComd;
sqlAdpt.Fill(dtst);
string[] cntName = new string[dtst.Tables[0].Rows.Count];
int i = 0;
try
{
foreach (DataRow rdr in dtst.Tables[0].Rows)
{
cntName.SetValue(rdr["Name"].ToString(), i);
i++;
}
}
catch { }
finally
{
sqlCon.Close();
}
return cntName;
}
}
我使用了bin文件夹中的AjaxToolkit .dll.我提供的代码plz告诉我此
I have use AjaxToolkit .dll which is in bin folder.I have provided the code plz tell me what is wrong in this
推荐答案
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AutoCompleteTest : System.Web.Services.WebService
出了什么问题
如果您如上所述取消web服务中的行注释,是否行得通?
Does it work if you uncomment the line in the webservice as above?
您应该使用字符串prefixtext和int count作为服务方法的参数,因为您使用的是ajax控件工具包. />
//Count用于指定显示的建议数.
public string [] GetCountriesList(string prefixText,int count)
You should use string prefixtext and int count as the parameters of the service method because you are using ajax control toolkit.
//Count is used to specify the number of suggestions shown.
public string[] GetCountriesList(string prefixText, int count)
这篇关于使用ajax的asp.net 3.5中的自动完成文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!