仿Google的搜索下拉框

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>无标题页</title>
<style>
ul,li {
    list-style:none;
 margin-left: 1px;
 margin-bottom:0;
 padding: 0;
 border: 0;
 list-style-type: none;
 list-style-image: none;
}
.show {
    z-index:99;
    font-size:12px;
    border:1px outset;
}
.show span {
    float:right;
}
.show div {
    width:100%;
    height:100%;
}
.show div ul li span {
    float:right;
}
.show div ul li {
    text-align:left;
    color:red;
    width:100%;
    cursor:default;
}
.blue {
    background:#0A246A;color:#ffffff;text-decoration:none;
}
.white {
    background:#ffffff;
}
</style>
<script type="text/javascript">
function CPos(x, y)
{
    this.x = x;
    this.y = y;
}
//获取控件的位置
function GetObjPos(ATarget)
{
    var target = ATarget;
    var pos = new CPos(target.offsetLeft, target.offsetTop);
   
    var target = target.offsetParent;
    while (target)
    {
        pos.x += target.offsetLeft;
        pos.y += target.offsetTop;
       
        target = target.offsetParent
    }
   
    return pos;
}
function showdivtxt(txtobj){
 showtxt.style.width=txtobj.offsetWidth;
 var p = GetObjPos(txtobj);
 showtxt.style.left=p.x;
 showtxt.style.top=p.y+txtobj.offsetHeight;
if(txtobj.value.replace(/(/s*$)/g, "")!=""){
//异步取数据
 var ds = _Default.SearchTxt(txtobj.value).value;
 var s;
 if (ds.Tables[0].Rows.length>0){
        s="<ul>";
     showtxt.style.display='block';
     for(var i=0;i<ds.Tables[0].Rows.length;i++){
         s+="<li class='white' οnclick=/"<%=this.tb_search.ClientID %>.value='"+ds.Tables[0].Rows[i]["tagword"]+"';showtxt.style.display='none';/" οnmοuseοut='changebgw(this);' οnmοuseοver='changebgb(this);'><span style='color:green;'>"+ds.Tables[0].Rows[i]["result"]+" 结果</span>"+ds.Tables[0].Rows[i]["tagword"]+"</li>";
     }
     s+="</ul>";
 }
 document.getElementById("txt09").innerHTML = s;
 }
 else{
 showtxt.style.display='none';
 }
}
function changebgb(li_obj){
    li_obj.className='blue';
}
function changebgw(li_obj){
    li_obj.className='white';
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="show" id="showtxt" style="display:none; position:absolute;"><div id="txt09"></div><span οnclick="showtxt.style.display='none';" style="cursor:hand;"><u>关闭[X]</u></span></div>
    <div>

<table width="90%"><tr><td><asp:TextBox ID="tb_search" runat="server" Width="200px"></asp:TextBox>
            <asp:Button ID="bt_search" runat="server" Text="搜 索" OnClick="bt_search_Click" /></td></tr></table>
</div>
    </form>
</body>
</html>

default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    public int flag;
    public string space;
    protected void Page_Load(object sender, EventArgs e)
    {

//注册
        AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
        this.tb_search.Attributes.Add("onkeyup", "showdivtxt(" + this.tb_search.ClientID + ");");
    }
    public void AddSearchWord(string word) {
        if (DBClass.ExecScalar("Select Count(*) From T_SearchReault Where tagword='" + word.Replace("'", "''") + "'", "") <= 0)
        {
            DBClass.ExecSQL("Insert Into T_SearchReault Values('" + word.Replace("'", "''") + "',1)");
        }
        else
        {
            DBClass.ExecSQL("Update T_SearchReault Set searchcount=searchcount+1 Where tagword='" + word.Replace("'", "''") + "'");
        }
    }
    [AjaxPro.AjaxMethod]
    public DataSet SearchTxt(string txt) {
        string Sql = @"select top 10 s.tagword,count(t.treename) result
                    from T_SearchReault s
                    left join T_Tree t on s.tagword=t.treename ";
        string order = @" group by s.tagword,s.searchcount order by s.searchcount desc";
        if(txt.Trim()!=""){
            Sql += " Where ";
        }
        string[] s_a = txt.Replace("'","''").Split(new char[] { ' ' });
        foreach (string s in s_a) {
            if (s != "") {
                Sql += " s.tagword like '%" + s.ToString() + "%'";
            }
        }
        Sql += order;
        DataSet ds = DBClass.getDataSet(Sql, "tb");
        return ds;
    }
    protected void bt_search_Click(object sender, EventArgs e)
    {
        AddSearchWord(tb_search.Text);
    }
}

sql

create table T_SearchReault(
id int identity(1,1),
tagword varchar(500) null,
searchcount int
)

CREATE TABLE [dbo].[T_Tree] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [treename] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [parentid] [int] NULL ,
 [treecode] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
 [url] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL

)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值