jquery autocomplete实现读取sql数据库自动补全TextBox

转自我本良人 原文 jquery autocomplete实现读取sql数据库自动补全TextBox

项目需要这样子一个功能,其他部门提的意见,只好去实现了哦,搞了好久才弄出来,分享一下。

1.前台页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>  
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
      
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head id="Head1" runat="server">  
        <title></title>  
        <script src="jquery-1.4.1-vsdoc.js" type="text/javascript"></script>  
        <script src="jquery.autocomplete.js" type="text/javascript"></script>  
        <link href="jquery.autocomplete.css" type="text/css" rel="stylesheet" />  
        <script language="javascript" type="text/javascript">  
            $(document).ready((function ()  
            {  
                $("#txtUser").autocomplete("GetCode.aspx");  
            }  
    ));  
        </script>  
    </head>  
    <body>  
        <form id="form1" runat="server">  
            <div>  
                用户名:  
                <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>  
            </div>  
        </form>  
    </body>  
    </html>  

2.GetCode.aspx

前台为空

后台代码:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Data;  
    using System.Data.SqlClient;  
      
    public partial class GetCode : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (Request.QueryString["q"] != null)  
            {  
                string key = Request.Params["q"].ToString();  
                string result = "";  
                SqlHelp sql = new SqlHelp();  
                string str = "select top 15 CustomCode from tCustomList where CustomCode like '" + key + "%'";  
                SqlDataReader dr = sql.ExecuteReader(str);  
                while (dr.Read())  
                {  
                    result += dr["CustomCode"].ToString() + "
";  
                }  
                dr.Dispose();  
                sql.SqlClose();  
                if (result == "")  
                    result = "not exists";  
                Response.Write(result);  
            }   
        }  
    }  

3. jquery.autocomplete.js

Download from jquery.autocomplete.js.rar

原文地址:https://www.cnblogs.com/arxive/p/6251117.html