实现类似于Goole的搜索匹配

前台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>
            &nbsp;&nbsp;
        </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,我发给你们

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值