asp.net 仿百度动态下拉菜单
最近要做一个仿百度动态下拉菜单的功能。网上资料挺多,不过本人较笨,弄了很久才搞定,分享给需要的朋友。
页面Js代码如下:
当然txtEmpno就是页面上的文本框了,我要做的是根据入职的信息动态在列表中显示用户编号和用户名。
<mce:script language="javascript" src="http://www.cnblogs.com/scripts/jquery.autocomplete.js" mce_src="scripts/jquery.autocomplete.js" type="text/javascript"></mce:script>
<mce:script type="text/javascript" language="javascript"><!--
$(document).ready(function() {
readyForEdit(false);
$("#txtEmpno").autocomplete(
"../Jspage/UserSet.aspx",
{
maxItemsToShow:20,
onItemSelect:selectItem1,
formatItem:formatItem1,
onFindValue:selectItem1,
width:400,
lineSeperator:'\n',
cellSeparator:'|',
minChars:1,
matchSubset:1,
cacheLength:10,
delay:200
});
$("#txtEmpno").focus();
});
function selectItem1(li){
$("#txtEmpno").val(li.selectValue);
readyForEdit(true);
}
function formatItem1(row){
return "编码:" + row[0] + " , 姓名:" + row[2] ;
}
function readyForEdit(edit){
}
// --></mce:script>
文件UserSet.aspx html部分不用填写,主要用来获取数据。cs文件内容
protected void Page_Load(object sender, EventArgs e)
{
ICommonAccess comAssess = DataFactory.GetInstance();
string key = Request.QueryString["q"];
try
{
StringBuilder sb = new StringBuilder();
sb.Append(" SELECT * from TUser ");
sb.Append(" where 1=1 and (usr_no like '%" + key + "%' or usr_name like '%" + key + "%' or usr_emp_no like '%" + key + "%') order by sn");
comAssess.Open();
DataTable dt = comAssess.GetDataTable(sb.ToString());
StringBuilder items = new StringBuilder();
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
items.Append(" " + dt.Rows[i]["usr_emp_no"].ToString() + "|" + dt.Rows[i]["usr_no"].ToString() + "|" + dt.Rows[i]["usr_name"].ToString() + " " + "\n");//\n
}
}
Response.Write(items.ToString());
Response.End();
}
catch (Exception ex)
{
string s = ex.Message;
}
finally
{
comAssess.Close();
}
}
生如夏花之灿烂