我想用ajax的AutoCompleteExtender控件实现类似google的搜索提示,但是老是没成功。下面提出我的代码,麻烦各位帮忙看下:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScript1" runat="server" ScriptMode="Release" >
</cc1:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="txtFind" ServiceMethod="GetCompletionList" CompletionSetCount="10" MinimumPrefixLength="1" EnableCaching="true"
ServicePath="WebService.asmx" Enabled="true" CompletionInterval="10" BehaviorID="AutoCompleteEx" >
</cc1:AutoCompleteExtender>
<asp:TextBox ID="txtFind" runat="server" Width="492px" BorderStyle="Solid" Height="24px" AutoPostBack="True" AutoCompleteType="Disabled"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel> using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Script.Services;//关键程序集引用
using System.IO;/// <summary>
///WebService 的摘要说明
/// </summary>[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[ScriptMethod]
[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey )
{
if (string.IsNullOrEmpty(prefixText) == true || count <= 0)
{
return null;
} int index=0;
string connString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection cn = new SqlConnection(connString);
SqlCommand cmd;
string cmdString ="select Pro_Keywords from Product where Pro_Keywords like '" + prefixText + "%'";
cmd = new SqlCommand(cmdString, cn);
cn.Open();
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable dt = ds.Tables[0]; List<string> returnData = new List<string>(count);
foreach (DataRow dr in dt.Rows)
{
string keywords = dr["Pro_Keywords"].ToString();
if (index < count && index < dt.Rows.Count) {
returnData.Add(keywords);
}
index++;
}
//myReader = cmd.ExecuteReader(); //while (myReader.Read())
//{
// returnData.Add(myReader.GetString(0));
//} return returnData.ToArray();
}
}
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScript1" runat="server" ScriptMode="Release" >
</cc1:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="txtFind" ServiceMethod="GetCompletionList" CompletionSetCount="10" MinimumPrefixLength="1" EnableCaching="true"
ServicePath="WebService.asmx" Enabled="true" CompletionInterval="10" BehaviorID="AutoCompleteEx" >
</cc1:AutoCompleteExtender>
<asp:TextBox ID="txtFind" runat="server" Width="492px" BorderStyle="Solid" Height="24px" AutoPostBack="True" AutoCompleteType="Disabled"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel> using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Script.Services;//关键程序集引用
using System.IO;/// <summary>
///WebService 的摘要说明
/// </summary>[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[ScriptMethod]
[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey )
{
if (string.IsNullOrEmpty(prefixText) == true || count <= 0)
{
return null;
} int index=0;
string connString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection cn = new SqlConnection(connString);
SqlCommand cmd;
string cmdString ="select Pro_Keywords from Product where Pro_Keywords like '" + prefixText + "%'";
cmd = new SqlCommand(cmdString, cn);
cn.Open();
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable dt = ds.Tables[0]; List<string> returnData = new List<string>(count);
foreach (DataRow dr in dt.Rows)
{
string keywords = dr["Pro_Keywords"].ToString();
if (index < count && index < dt.Rows.Count) {
returnData.Add(keywords);
}
index++;
}
//myReader = cmd.ExecuteReader(); //while (myReader.Read())
//{
// returnData.Add(myReader.GetString(0));
//} return returnData.ToArray();
}
}
具体没看出错误来...ServicePath这个路径确认一下。。看有不有问题参考。。
想起来了。。 public string[] GetCompletionList(string prefixText, int count, string contextKey )//这个红色方法的这个参数是不是多了啊。。这方法好像就规定必需是string prefixText, int count吧好像拼写都不能有误
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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 runat="server">
<title>AJAX控件之AutoComplete</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:TextBox ID="MyAuto" runat="server" Width="191px"></asp:TextBox><br />
<br />
<cc1:AutoCompleteExtender ID="ace" runat="server" CompletionInterval="100" MinimumPrefixLength="1"
ServiceMethod="GetCompletionList" ServicePath="WebService.asmx" TargetControlID="MyAuto">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html> # using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//用以调用Web Service方法签名
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
//if (count == 0)
//{
// count = 10;
//}
Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65,90);
char c2 = (char)random.Next(97, 122);
char c3 = (char)random.Next(48, 57);
char c4 = (char)random.Next(33, 43);
items.Add(prefixText + c1 + c2 + c3 + c4);
}
return items.ToArray();
}
}
用我的控件吧,方便多了