转:ashx+jquery+autocomplete.js实现自动填充

原文地址:http://www.cnblogs.com/doublecc/archive/2009/12/05/1617381.html

前台页面代码:

<script type="text/javascript" src="../js/jquery.js" ></script>
<script type="text/javascript" src="../js/jquery.autocomplete.js"></script>
<script src="../js/thickbox.js" type="text/javascript"></script>
 <link href="../css/thickbox.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="../css/jquery.autocomplete.css"  rel="Stylesheet"/>
<script type="text/javascript">
   //获取企业输入框ID
    var txtCorporation = "#<%=txtCorporation.ClientID %>";
   
    function findValue(li)
     { 
        if (li == null) return alert("No match!");
        if (!!li.extra)
        var sValue =unescape(li.extra[0]);
     
    }
    function selectItem(li)
     {
        findValue(li);
     }
  
    $(document).ready(function()
     {
       
        $(txtCorporation).autocomplete("ListCorporation.ashx",
          {
            delay: 10,
            minChars: 1,
            matchSubset: 1,
            cacheLength: 1,
            onItemSelect: selectItem,
            onFindValue: findValue,
            autoFill: true,
            maxItemsToShow: 20
        });
    });   
</script>

<div>

<asp:TextBox id="txtCorporation" runat="server" ></asp:TextBox>

</div>

 后台页面代码:

<%@ WebHandler Language="C#" Class="ListCorporation" %>

using System;
using System.Web;
using System.Text;
using System.Data;
public class ListCorporation :
    IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        if (context.Request.QueryString["q"] != null)
        {
            string key =context.Request.QueryString["q"];
            context.Response.Write(GetCorporationList(key));
        }
      
       
    }
    public string GetCorporationList(string key)
    {
        com.hbwl.BLL.base_Corporation ListCorporation = new com.hbwl.BLL.base_Corporation();
        StringBuilder strWhere = new StringBuilder("1=1");
        strWhere.Append(" and CorporationName like '%").Append(key).Append("%'");
        DataTable dt = ListCorporation.GetList(10,1,strWhere.ToString()).Tables[0];
        string result = "";
           if(dt.Rows.Count>0)
            {
           
                StringBuilder items = new StringBuilder();
                //历遍所有的海关编码数据
                foreach (DataRow dr in dt.Rows)
                {
                    items.Append(dr["CorporationName"].ToString() + "\n");
                }
                result = items.ToString();
            }
            return result;
    } 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

posted @ 2010-02-03 08:50  mysun  阅读(3298)  评论(2编辑  收藏  举报