DropDownList模拟Google搜索框
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="t.aspx.cs" Inherits="t" %>
<!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>seach</title>
<script type="text/javascript" >
function parseShop(data)
{
var xmlDoc=loadXML(data);
var xSel=xmlDoc.getElementsByTagName('mx');
var sel= document.getElementById('ddl_shop');
var val=sel.options[0].text;
var opt=new Option(val,'',false,true);
sel.options.length=0;
sel.add(opt);
for(var i=0;i<xSel.length;i++)
{
var xText=xSel[i].childNodes[0].firstChild.nodeValue;
var xValue=xSel[i].childNodes[1].firstChild.nodeValue;
var option=new Option(xText,xValue);
try
{
sel.add(option);
}
catch(e)
{
}
}
sel.selectedIndex=0;
sel.onclick=function(){};
}
function loadXML(text)
{
var xmlDoc;
try
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(text);
}
catch(e)
{
try
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(text,"text/xml");
}
catch(e)
{
alert(e.message)
}
}
return xmlDoc;
}
function catch_keydown(sel)
{
switch(event.keyCode)
{
case 13:
sel.click();
event.returnValue = false;
break;
case 27:
case 46:
sel.selectedIndex=0;
event.returnValue = false;
break;
case 8:
sel.selectedIndex=0;
var val=sel.options[0].text;
if(val.length>0)
{
sel.options[0].text=val.substr(0,val.length-1);
}
event.returnValue = false;
break;
}
}
var shophttp;
function catch_press(sel)
{
sel.selectedIndex=0;
sel.options[0].text =sel.options[0].text+ String.fromCharCode(event.keyCode);
var key=sel.options[0].text;
var url ="t.ashx?key=" + encodeURI(key) + "&time=" + new Date().toString();
if(window.ActiveXObject)
{
shophttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
shophttp = new XMLHttpRequest();
}
shophttp.onreadystatechange =getOver;
shophttp.open("GET",url,true);
shophttp.setRequestHeader("If-Modified-Since","0");
shophttp.send("");
event.returnValue = false;
}
function getOver()
{
if(shophttp.readyState==4)
{
if(shophttp.status==200)
{
var data = shophttp.responseText;
if (data!='')
{
parseShop(data);
}
}
}
}
</script>
</head>
<body >
<form id="form1" runat="server">
<asp:DropDownList onkeypress="catch_press(this);" onkeydown="catch_keydown(this)" id="ddl_shop" runat="server"></asp:DropDownList>
</form>
</body>
</html>
************************************************************
cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "select top 10 yh_id,''+yh_sjh+' | '+simplename as name,simplename from v_Company_info order by simplename";
DataSet ds = zhan.Data.Sql.PubClass.ExecuteDataSet("", sql);
ddl_shop.DataTextField = "name";
ddl_shop.DataValueField = "yh_id";
ddl_shop.DataSource = ds;
ddl_shop.DataBind();
ddl_shop.Items.Insert(0,new ListItem("", ""));
ddl_shop.SelectedIndex = 0;
}
}
************************************************************
ashx:
<%@ WebHandler Language="C#" Class="t" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
public class t : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/plain";
string Key = context.Request["key"];
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version='1.0' encoding='utf-8' ?>");
sb.Append("<mxlist>");
string sql = string.Format("select top 10 yh_id,''+yh_sjh+' | '+simplename as name,simplename from v_Company_info where yh_sjh like '{0}%' or simplename like '{0}%' order by simplename", Key);
if (string.IsNullOrEmpty(Key))
{
sql = "select top 10 yh_id,''+yh_sjh+' | '+simplename as name,simplename from v_Company_info order by simplename";
}
SqlParameter para = new SqlParameter("@key", SqlDbType.VarChar);
para.Value = Key;
DataSet ds = zhan.Data.Sql.PubClass.ExecuteDataSet("", sql);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string typename = Login.DoXml(ds.Tables[0].Rows[i]["name"].ToString());
typename = string.IsNullOrEmpty(typename) == true ? HttpUtility.HtmlEncode(" ") : typename;
sb.Append("<mx>");
sb.Append("<typename>" + typename + "</typename>");
sb.Append("<typevalue>" + Login.DoXml(ds.Tables[0].Rows[i]["yh_id"].ToString()) + "</typevalue>");
sb.Append("</mx>");
}
}
sb.Append("</mxlist>");
context.Response.Write(sb.ToString());
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}