如百度一样的搜索智能提示(显示数据库的数据)

1、body部分

 1 <body>
 2     <form id="form1" runat="server">
 3
 4     <div  style="margin-left:450px">
 5
 6         <asp:TextBox ID="txtCode"  Width="148px" onkeyup="SelectTip()"   runat="server"></asp:TextBox><br />
 7
 8         <div id="divShowText" style="width: 150px;"></div><br />
 9
10   </div>
11     </form>
12 </body>  

2、jquery 部分

 1  <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
 2     <script type="text/javascript">
 3
 4         $(document).ready(function () {
 5
 6             $("li").live("click", function () {
 7                 $("#txtCode").val($(this).text());
 8
 9                 $("#divShowText").html("");
10             })
11
12             $("li").live({
13                 mouseenter: function () {
14                     $(this).css("background-color", "gray"); //鼠标移入事件
15                 },
16                 mouseleave: function () {
17                     $(this).css("background-color", "white"); //鼠标移出事件
18                 }
19             });
20         });
21
22         $(document).ready(function () {
23             setInterval("startRequest()", 100);
24         });
25         function startRequest() {
26             document.URL = location.href;
27         }
28
29         //Ajax请求
30         function SelectTip() {
31             var temp = $("#txtCode").val();
32             if (temp == "" || temp == null) {
33                 $(divShowText).html("");
34             }
35             else {
36                 $.ajax({
37                     type: "post",
38                     url: "MusicHandler.ashx?methodType=code",//要跳转的中性方法的页面
39                     data: { code: temp, methodType: "code" },//参数
40                     success: function (result) {
41                         $(divShowText).html("");
42                         $(divShowText).html(result);
43                     },
44                     //请求执行异常执行的方法
45                     error: function (XMLHttpRequest, textStatus, errorThrown) {
46                         //alert(XMLHttpRequest.status);
47                         $(divShowText).html("");
48                         $(divShowText).html("<lable color=‘red‘>异常,请关闭页面重试,或联系管理员</lable>");
49                     }
50                 });
51             }
52         }
53
54     </script>

3、Stytle样式

 1 <style type="text/css">
 2         li {
 3             text-decoration: none;
 4             display: block;
 5             list-style: none;
 6             cursor: pointer;
 7             padding: 0px;
 8             margin: 0px;
 9         }
10
11         ul {
12             display: block;
13             list-style: none;
14             margin: 0px;
15             padding: 0px;
16         }
17     </style>  

4、MusicHandler.ashx 一般处理程序页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace Combobox
{
    /// <summary>
    /// MusicHandler 的摘要说明
    /// </summary>
    public class MusicHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string html = "";

            //在这里,参数获取方式为context.Request.Params["methodType"].ToString()
            switch (context.Request["methodType"].ToString())
            {
                case "code":
                    html = CodeHandler(context.Request.Params["code"].ToString());
                    break;
                case "user":
                    break;
            }
            context.Response.Write(html);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        //把数据库的数据读出并转换成 List<string>
        public List<string> GetData(string mname)
        {
            string sql = "SELECT name FROM MusicInfo_Table where name like ‘%" + mname + "%‘";
            string connectionString = "Data Source=.;Initial Catalog=MusicDataBase;User ID=sa;Password=manager";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataReader dr = cmd.ExecuteReader();
            //MusicInfo_Table 是表的实例类
            List<MusicInfo_Table> list = new List<MusicInfo_Table>();
            while (dr.Read())
            {
                MusicInfo_Table mt = new MusicInfo_Table();
                mt.Name = dr["name"].ToString();
                list.Add(mt);
            }
            List<string> list1 = new List<string>(list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                list1.Add(list[i].Name.ToString());
            }
            conn.Close();
            return list1;

        }

        //把读出的数据显示成下拉框模式
        public string CodeHandler(string code)
        {
            List<string> list = GetData(code);//请将此处理解为:向数据库请求和以当前参数开头的所有数据,返回为字符串列表
            string html = "<ul>";
            foreach (string temp in list)
            {
                html = html + "<li>" + temp + "</li>";
            }
            html = html + "</ul>";
            return html;
        }
    }
}

  

时间: 2024-08-30 08:06:47

如百度一样的搜索智能提示(显示数据库的数据)的相关文章

JQuery AutoComplete搜索智能提示

首先需要引入的文件有: jquery-ui-1.10.4.custom.min.cssjquery-ui-1.10.4.custom.min.js 然后在后台组织json格式的数据,组织成一个list型数据String id;String label;String value; 如: var availableTagsJSON = [ { label: "C#", value: "C#", id: "1" }, { label: "C+

jQuery实现TEXT文本框输入时的提示信息(谷歌百度淘宝搜索框提示实现)

在搜索框中,输入之前框内有输入的提示信息,文本框获得焦点后会自动消失的效果,效果图如下: 鼠标放在文本框时的效果: 创建工具类(已经存在就不用创建了)Util.js(DWR的JS) 在里面添加如下方法: Js代码 /** * Input框里的灰色提示,使用前先引入jquery * <br>使用方法:<input type="text" tipMsg="您的用户名"   /> * * @return */ function inputTipTe

Servlet+Ajax实现搜索智能提示

一般在百度搜索框输入关键词时,会弹出一些相关信息提示,方便点选: 页面(search.jsp): 1 <input type="text" name="keyWords" id="keyWords" style="width:200px; height:20px;" /> 2 <input type="button" id="button" value="百度

c#运用TreeView控件的树形视图显示数据库中数据

TreeView控件显示数据库,Nodes集合的Add方法. Treeview控件的Nodes集合包含多个子节点,节点也可以包含另外的节点.通过节点的backcolor属性设置背景色. 具体代码测试: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System

MySQL 创建、删除、显示数据库、数据表

1 创建.删除.显示数据库 -- 创建数据库 create database student_db character set utf8 collate utf8_general_ci; -- 删除数据库 drop database student_db; -- 显示所有数据库 show databases; -- 选择数据库 use student_db; 2 创建.删除.显示数据表 -- 创建表 create table student( id int unsigned not null p

在Winfrom下实现类似百度、Google搜索自能提示功能

前记:数据源来自页面的一个ComboBox的数据源List<Contract>集合 页面放置一个TextBox(搜索框).ListBox(显示搜索出来的数据),ListBox位置位于TextBox正下方,初始化隐藏. TextBox--->txtSearch  ListBox--->listBox1关于两个控件的事件,详见代码: [c-sharp] view plain copy #region  自能提示 Point pList = new Point(); private vo

AJAX实现搜索智能提示

<!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> <meta http-equiv="Content-

接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码

-- ------------------------------ Table structure for articles-- ----------------------------DROP TABLE IF EXISTS `articles`;CREATE TABLE `articles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL COMMENT '标题', `content` text CO

美团搜索-搜索引擎关键字智能提示的一种实现[转]

http://tech.meituan.com/pinyin-suggest.html --------------------------------------------------------------------- 快照: 问题背景 搜索关键字智能提示是一个搜索应用的标配,主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户快速查找到目标商家,我们基于solrcloud实现了商家搜索模块.用户在查找商