.NET TEXTBOX 动态提示功能

8 篇文章 0 订阅
 

跟google你输入关键字,动态显示大概条数的提示,首先看下效果

第一步css:

  1. #dSuggest {   
  2.     z-index:0;   
  3.     position:absolute;   
  4.     background-color:#ffffff;   
  5. }   
  6. #sug {   
  7.     list-style:none;   
  8.     border:solid 1px #09f;   
  9.     margin-left:0;   
  10.     margin-top:0;   
  11.     width:310px;   
  12. }   
  13. #sug li {   
  14.     width:100%;   
  15.     padding-left:2px;   
  16. }   
  17. #sug span {   
  18.     position:absolute;   
  19.     z-index:200;   
  20.     right:3px;   
  21. }   
  22. .select {   
  23.     background-color:#09f;   
  24.     color:#ffffff;   
  25.     cursor:default;   
  26. }  
    #dSuggest {
        z-index:0;
        position:absolute;
        background-color:#ffffff;
    }
    #sug {
        list-style:none;
        border:solid 1px #09f;
        margin-left:0;
        margin-top:0;
        width:310px;
    }
    #sug li {
        width:100%;
        padding-left:2px;
    }
    #sug span {
        position:absolute;
        z-index:200;
        right:3px;
    }
    .select {
        background-color:#09f;
        color:#ffffff;
        cursor:default;
    }

第二步javascript:

  1.  var XMLHttp=false;   
  2.  var j=-1;   
  3.  function CreateXMLHttpRequest() {   
  4.      if(window.ActiveXObject)   
  5.      {   
  6.          XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");   
  7.      }   
  8.      else if(window.XMLHttpRequest)   
  9.      {   
  10.          XMLHttp = new XMLHttpRequest();   
  11.      }   
  12.      if(!XMLHttp && typeof XMLHttpRequest != 'undefined') {   
  13.          XMLHttp = new XMLHttpRequest();   
  14.      }   
  15.  }   
  16.   
  17.  function GetSuggest(keyword,e) {   
  18.      var keyc=GetKeyCode(e);   
  19.   
  20.      if(keyc==40 || keyc==38 || keyc==13)   
  21.          return;   
  22.      var _url="Search.ashx?keyword="+escape(keyword);   
  23.   
  24.      CreateXMLHttpRequest();   
  25.      XMLHttp.onreadystatechange=function(){   
  26.          if(XMLHttp.readystate==4) {   
  27.              if(XMLHttp.status==200) {   
  28.                  $("dSuggest").innerHTML=XMLHttp.responseText;   
  29.                  j=-1;   
  30.              }   
  31.          }   
  32.      }   
  33.      XMLHttp.open("get",_url,true);   
  34.      XMLHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");   
  35.      XMLHttp.send(null);   
  36.  }   
  37.   
  38.  function set_style(num) {//设置样式   
  39.     for(var i=0;i< $$().length;i++) {   
  40.         if(i>=0 && i< $$().length&&i==num) {   
  41.              $$()[i].className="select";   
  42.         }   
  43.         else  
  44.         {   
  45.            $$()[i].className="";   
  46.         }   
  47.        }   
  48.   
  49.  }   
  50.   
  51.  function mo(nodevalue) {   
  52.      j=nodevalue;   
  53.      set_style(j);   
  54.  }   
  55.   
  56.  function form_submit() {   
  57.      var lis=$$();   
  58.      if(lis==null)   
  59.          return;   
  60.      for(var i=0;i   
  61. S.LENGTH;I++) key='lis[i].innerHTML.substring(0,lis[i].innerHTML.indexOf("<SPAN' var { if(lis[i].className='="select")'>"));   
  62.              $('ctl00_ContentPlaceHolder1_txtKeyword').value=key;   
  63.              $("dSuggest").innerHTML="";   
  64.          }   
  65.      }   
  66.   
  67.      form_submits();   
  68.  }   
  69.   
  70.  function form_submits() {   
  71.       var stainfo = $("ctl00_ContentPlaceHolder1_stainfo").value;   
  72.       var pno = $("ctl00_ContentPlaceHolder1_txtKeyword").value;   
  73.       if(stainfo == "in")   
  74.       {   
  75.          window.location = "inStock.aspx?sta=in&pno="+pno;   
  76.       }   
  77.       else  
  78.       {   
  79.          window.location = "inStock.aspx?pno="+pno;   
  80.       }   
  81.  }   
  82.   
  83.  function cleanBox()   
  84.  {   
  85.      $("dSuggest").innerHTML="";   
  86.  }   
  87.   
  88.  function updown(e) {   
  89.      var keyc=GetKeyCode(e);   
  90.   
  91.      if(keyc==40 || keyc==38) {   
  92.          if($$()==null)   
  93.              return;   
  94.          if(keyc==40) {   
  95.              j++;   
  96.              if(j>=$$().length)   
  97.              {   
  98.                j=0;   
  99.              }   
  100.          } else if(keyc==38) {   
  101.              j--;   
  102.              if(j==-1)   
  103.                  j=$$().length-1;   
  104.   
  105.          }   
  106.          set_style(j);   
  107.      }   
  108.  }   
  109.   
  110.  function GetKeyCode(e) {//取得不同浏览器下的键盘事件值   
  111.      var keyc;   
  112.      if(window.event) {//ie键盘事件   
  113.    keyc=e.keyCode;   
  114.   } else if (e.which) {//火狐   
  115. keyc=e.which;   
  116.   }   
  117.   return keyc;   
  118.  }   
  119.   
  120.  function $(id) {   
  121.      return document.getElementById(id);   
  122.  }   
  123.   
  124.  function $$() {   
  125.      var ul=$("sug");   
  126.      if(ul==null)   
  127.          return null;   
  128.      return ul.getElementsByTagName("li");   
  129.  }   
  130.  //getPos(this,'Left'),getPos(this,'Top')+this.offsetHeight   
  131.  function getPos(el,sProp) {   
  132.      var iPos = 0;   
  133.      while (el!=null) {   
  134.          iPos+=el["offset" + sProp];   
  135.          el = el.offsetParent;   
  136.      }   
  137.      return iPos;   
  138.  }   
  139.  function setPosition(obj) {   
  140.      $('dSuggest').style.left=getPos(obj,'Left');   
  141.      $('dSuggest').style.top=getPos(obj,'Top')+obj.offsetHeight   
  142.  }  
    var XMLHttp=false;
    var j=-1;
    function CreateXMLHttpRequest() {
        if(window.ActiveXObject)
        {
            XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if(window.XMLHttpRequest)
        {
            XMLHttp = new XMLHttpRequest();
        }
        if(!XMLHttp && typeof XMLHttpRequest != 'undefined') {
            XMLHttp = new XMLHttpRequest();
        }
    }

    function GetSuggest(keyword,e) {
        var keyc=GetKeyCode(e);

        if(keyc==40 || keyc==38 || keyc==13)
            return;
        var _url="Search.ashx?keyword="+escape(keyword);

        CreateXMLHttpRequest();
        XMLHttp.onreadystatechange=function(){
            if(XMLHttp.readystate==4) {
                if(XMLHttp.status==200) {
                    $("dSuggest").innerHTML=XMLHttp.responseText;
                    j=-1;
                }
            }
        }
        XMLHttp.open("get",_url,true);
        XMLHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        XMLHttp.send(null);
    }

    function set_style(num) {//设置样式
       for(var i=0;i< $$().length;i++) {
           if(i>=0 && i< $$().length&&i==num) {
                $$()[i].className="select";
           }
           else
           {
              $$()[i].className="";
           }
          }

    }

    function mo(nodevalue) {
        j=nodevalue;
        set_style(j);
    }

    function form_submit() {
        var lis=$$();
        if(lis==null)
            return;
        for(var i=0;i
"));
                $('ctl00_ContentPlaceHolder1_txtKeyword').value=key;
                $("dSuggest").innerHTML="";
            }
        }

        form_submits();
    }

    function form_submits() {
         var stainfo = $("ctl00_ContentPlaceHolder1_stainfo").value;
         var pno = $("ctl00_ContentPlaceHolder1_txtKeyword").value;
         if(stainfo == "in")
         {
            window.location = "inStock.aspx?sta=in&pno="+pno;
         }
         else
         {
            window.location = "inStock.aspx?pno="+pno;
         }
    }

    function cleanBox()
    {
        $("dSuggest").innerHTML="";
    }

    function updown(e) {
        var keyc=GetKeyCode(e);

        if(keyc==40 || keyc==38) {
            if($$()==null)
                return;
            if(keyc==40) {
                j++;
                if(j>=$$().length)
                {
                  j=0;
                }
            } else if(keyc==38) {
                j--;
                if(j==-1)
                    j=$$().length-1;

            }
            set_style(j);
        }
    }

    function GetKeyCode(e) {//取得不同浏览器下的键盘事件值
        var keyc;
        if(window.event) {//ie键盘事件
		    keyc=e.keyCode;
	    } else if (e.which) {//火狐
			keyc=e.which;
	    }
	    return keyc;
    }

    function $(id) {
        return document.getElementById(id);
    }

    function $$() {
        var ul=$("sug");
        if(ul==null)
            return null;
        return ul.getElementsByTagName("li");
    }
    //getPos(this,'Left'),getPos(this,'Top')+this.offsetHeight
    function getPos(el,sProp) {
        var iPos = 0;
        while (el!=null) {
            iPos+=el["offset" + sProp];
            el = el.offsetParent;
        }
        return iPos;
    }
    function setPosition(obj) {
        $('dSuggest').style.left=getPos(obj,'Left');
        $('dSuggest').style.top=getPos(obj,'Top')+obj.offsetHeight
    }

第三步:ASPX页面设置

  1.   
  2. <FORM id=form1 onkeypress="if(GetKeyCode(event)==13){return false;}" runat="server" 标签>  
  3. <INPUT style="WIDTH: 310px" onkeydown=if(GetKeyCode(event)==13)form_submit(); id=txtKeyword onkeyup=GetSuggest(this.value,event);updown(event) runat="server" autocomplete="off">  
  4. <DIV id=dSuggest></DIV>  
  5. </FORM>  

  
  

第四步:创建文件search.ashx

  1. < %@ WebHandler Language="C#" Class="Search" %>   
  2.   
  3. using System;   
  4. using System.Web;   
  5.   
  6. using System.Data.SqlClient;   
  7.   
  8. public class Search : IHttpHandler {   
  9.   
  10.     public void ProcessRequest (HttpContext context) {   
  11.         context.Response.ContentType = "text/plain";   
  12.         string sKeyword = context.Request["keyword"];   
  13.         if (string.IsNullOrEmpty(sKeyword))   
  14.         {   
  15.             context.Response.End();   
  16.         }   
  17.         SqlConnection conn = new SqlConnection("Server=(local);User ID=sa;Password=123;DataBase=CMStock");   
  18.         SqlCommand cmd = new SqlCommand("select top 20 * from Product where pno like '" + sKeyword + "%' OR name like '%" + sKeyword + "%';", conn);   
  19.         conn.Open();   
  20.         SqlDataReader dr = cmd.ExecuteReader();   
  21.         if (dr.HasRows)   
  22.         {   
  23.             context.Response.Write("   
  24. <UL id=sug>");   
  25.             int i = 0;   
  26.             while (dr.Read())   
  27.             {   
  28.                 string pno = dr["pno"].ToString();   
  29.                 if (pno.Length > 15)   
  30.                 {   
  31.                     pno = pno.Substring(0, 15) + "...";   
  32.                 }   
  33.                 string pname = dr["name"].ToString();   
  34.                 if (pname.Length > 30)   
  35.                 {   
  36.                     pname = pname.Substring(0, 30) + "...";   
  37.                 }   
  38.                 context.Response.Write(string.Format("   
  39. <LI οnmοuseοver='\"mo(this.value)\"' οnclick='\"form_submit()\"' value={0}>{1}<SPAN>{2}</SPAN>   
  40.   
  41. ", i, pno, pname));   
  42.                 i++;   
  43.             }   
  44.             context.Response.Write("</LI></UL>   
  45.   
  46. ");   
  47.         }   
  48.         else  
  49.         {   
  50.   
  51.             context.Response.Write("   
  52. <UL id=sug>");   
  53.             context.Response.Write(string.Format("   
  54. <LI οnmοuseοver='\"mo(this.value)\"' οnclick='\"form_submit()\"' value={0}>{1}<SPAN>{2}</SPAN>   
  55.   
  56. ", 0, sKeyword, "该商品货号货名称错误,请更换!"));   
  57.             context.Response.Write("</LI></UL>   
  58.   
  59. ");   
  60.         }   
  61.   
  62.         conn.Close();   
  63.         dr.Close();   
  64.         cmd.Dispose();   
  65.     }   
  66.   
  67.     public bool IsReusable {   
  68.         get {   
  69.             return false;   
  70.         }   
  71.     }   
  72.   
  73. }  
< %@ WebHandler Language="C#" Class="Search" %>

using System;
using System.Web;

using System.Data.SqlClient;

public class Search : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string sKeyword = context.Request["keyword"];
        if (string.IsNullOrEmpty(sKeyword))
        {
            context.Response.End();
        }
        SqlConnection conn = new SqlConnection("Server=(local);User ID=sa;Password=123;DataBase=CMStock");
        SqlCommand cmd = new SqlCommand("select top 20 * from Product where pno like '" + sKeyword + "%' OR name like '%" + sKeyword + "%';", conn);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            context.Response.Write("

 
 
  • "); int i = 0; while (dr.Read()) { string pno = dr["pno"].ToString(); if (pno.Length > 15) { pno = pno.Substring(0, 15) + "..."; } string pname = dr["name"].ToString(); if (pname.Length > 30) { pname = pname.Substring(0, 30) + "..."; } context.Response.Write(string.Format("
  • {1}{2} ", i, pno, pname)); i++; } context.Response.Write("
"); } else { context.Response.Write("
  • "); context.Response.Write(string.Format("
  • {1}{2} ", 0, sKeyword, "该商品货号货名称错误,请更换!")); context.Response.Write("
"); } conn.Close(); dr.Close(); cmd.Dispose(); } public bool IsReusable { get { return false; } } }

运行 收工!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值