前台aspx代码:
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div style="text-align:center">
<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" EnableCaching="true" >
</cc1:AutoCompleteExtender>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
需要使用Webservices服务,下面是我写的Webservices:
using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
/// <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 () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
SqlHelp help=new SqlHelp();
//System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count);
List<string> list = new List<string>(count);
DataSet ds = new DataSet();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Name", SqlDbType.NVarChar);
param[0].Value = prefixText;
ds = help.ReturnDSP("seach", param);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
list.Add(ds.Tables[0].Rows[i][0].ToString());
}
return list.ToArray();
}
}
上面SqlHelp help=new SqlHelp();是我写的数据访问层方法,大家可以替换成自己的方法即可。
需要使用微软的AJAX控件:AutoCompleteExtender;大家下不到,可以给我发邮件:1053_23xuwei@163.com,我发给你们