仿google查询效果

今天没事做,闲得无聊做了个玩玩,希望对刚学的人有点帮助!

前台: 

<%@ Page Language="C#" AutoEventWireup="true"  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>Untitled Page</title>
    <script type="text/javascript">
    var xmlHttp;
    function createXmlRequest()
    {
         try {
          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e2) {
            xmlHttp = false;
          }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
          xmlHttp = new XMLHttpRequest();
        }
    }
   
    function handle()
    {
        var key=document.getElementById("txtSearch").value;
        createXmlRequest();
        var url = "Handle.aspx?key="+key;
        xmlHttp.open("GET",url,true);
        xmlHttp.onreadystatechange = show;
        xmlHttp.send(null);
        if(document.getElementById("txtSearch").value=="")
        {
            document.getElementById("showResult").innerText = "";
            document.getElementById("showResult").style.visibility = "hidden";
        }
    }
   
    function show()
    {
        if(xmlHttp.readyState == 4)
        {
            if(xmlHttp.status == 200)
            {
                var showResult = "";
                var result = xmlHttp.responseText.split(",");
               
                var Abe = getLTWH(document.getElementById("txtSearch"));
                if(xmlHttp.responseText != "")
                {
                    document.getElementById("showResult").style.visibility = "visible";
                }
                document.getElementById("showResult").style.left=Abe.left;
                document.getElementById("showResult").style.top=Abe.top + Abe.height;
                document.getElementById("showResult").style.width=Abe.width - 2;
                document.getElementById("showResult").style.height=Abe.height * result.length;
               
                for(i=0;i<result.length;i++)
                {
                    showResult += result[i] + "<br />"
                }
                document.getElementById("showResult").innerHTML = showResult;
            }
        }
    }
   
    function getLTWH(element)
    {
        if ( arguments.length != 1 || element == null )
        {
            return null;
        }
        var offsetTop = element.offsetTop;
        var offsetLeft = element.offsetLeft;
        var offsetWidth = element.offsetWidth;
        var offsetHeight = element.offsetHeight;
        while( element = element.offsetParent )
        {
            offsetTop += element.offsetTop;
            offsetLeft += element.offsetLeft;
        }
        var Abe={
           left:offsetLeft,
           top:offsetTop,
           width:offsetWidth,
           height:offsetHeight
        }
        return Abe;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="showResult"
            style="background-color: #6699FF; border: 1px solid #C0C0C0; position: absolute; visibility:hidden"  ></div>
    <input type="text" id="txtSearch" name="txtSearch" οnkeyup="handle()" />
    </div>
    </form>
</body>
</html>

 

后台:

string key = Request.QueryString["key"].ToString();
        string result = string.Empty;
        SqlConnection conn = new SqlConnection("Data Source=.//SQLEXPRESS;Initial Catalog=qq;Integrated Security=SSPI;");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select userName from users where userName like '" + key + "%'";
        cmd.Connection = conn;
        conn.Open();
        SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (sdr.Read())
        {
            result += sdr.GetString(0) + ",";
        }
        result = result.TrimEnd(',');
        Response.Write(result);
        Response.End();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值