javascript 異步頁面查詢實現(xiàn)代碼(asp.net)
更新時間:2010年05月26日 22:02:40 作者:
異步頁面查詢,其實與自動完成時一樣的原理,根據用戶輸入的關鍵詞自動的與數(shù)據庫中的匹配,并顯示出來,提高用戶體驗,但主要搜索量大的話,增加服務器開銷。
1。 testlist.aspx頁面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="jscript/autoSuggest.css" type="text/css"/>
<link rel="stylesheet" href="jscript/ac.css" type="text/css"/>
<script language="Javascript" src="jscript/autoSuggest.js"></script>
<script language="Javascript" src="jscript/ac.js"></script>
function ACClient( queryurl, width, objInput, objLable )
{
this.xmldom = getXmlhttp();
this.queryurl = queryurl;
this.prolist = new Array();
this.divwidth = width;
this.objInput = objInput;
this.getEligible = function(ac) {
var eligible = new Array();
if (ac.inputText.length < 1) {
document.getElementById(objInput.name).value = "";
document.getElementById(objLable.name).value = "";
return eligible;
}
var strURL = this.queryurl + escape(ac.inputText) + "&rand=" + Math.random();
this.xmldom.open("GET", strURL, false);
try {
this.xmldom.send();
}
catch (e) {
return;
}
this.prolist = this.xmldom.responseXML.documentElement.selectNodes("/duxion/object");
if (0 == this.prolist.length) {
return eligible;
}
for (var i = 0; i < this.prolist.length; i++) {
if (i > 15)
break;
var node = this.prolist.item(i);
eligible[eligible.length] = node.getAttribute("fullcontent");
}
ac.div.style.width = this.divwidth;
return eligible;
};
this.useSuggestion = function( sel )
{
if( sel>=0 )
{
var node = this.prolist.item(sel);
if(typeof(node.getAttribute( "value" )) != "undefined")
document.getElementById(objInput.name).value = node.getAttribute( "value" );
else
document.getElementById(objInput.name).value = "";
if(typeof(node.getAttribute( "content" )) != "undefined")
document.getElementById(objLable.name).value = node.getAttribute( "content" );
else
document.getElementById(objLable.name).value = "";
if (this.objInput.name == "hidfreq_code") {
document.getElementById("txtfreq_name").value = node.getAttribute("Name");
document.getElementById("hidfreq_code").value = node.getAttribute("Code");
}
}
};
return this;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<%--div 用于顯示下拉查詢部分--%>
<div id="autosuggest" style="width:160px;font-size:12px;"><ul></ul></div>
<div>
<asp:TextBox ID="txtfreq_name" CssClass="needtext" runat="server" Width="100px"></asp:TextBox><asp:HiddenField
ID="hidfreq_code" runat="server" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
new AutoSuggest(document.getElementById('txtfreq_name'),
new ACClient("testXml.aspx?tag=yp_frequency&value=", "200px", document.getElementById('hidfreq_code'), document.getElementById('txtfreq_name')));
</script>
2。 供查詢的xml的testxml.aspx.cs文件
public partial class testXml : BaseForm
{
protected void Page_Load(object sender, EventArgs e)
{
ListXmlData();
}
private string[] arrwidth;
private string[] arrfield;
public string fieldlist = "區(qū)域Id,區(qū)域名,區(qū)域編碼,區(qū)域層次";
public string coloumwidth = "60,10,20,30";
//顯示xml格式數(shù)據
private void ListXmlData()
{
//各字段的寬度
string strwhere="";
if (Request["value"] != null)
{
strwhere = string.Format(" and Code like '{0}%'", Request["value"].ToString());
}
arrfield = fieldlist.Split(',');
arrwidth = coloumwidth.Split(',');
DataTable dtList = new Districts().Search("1=1" + strwhere);// dis // SqlHelper.GetTable(subsys_db, CommandType.Text, strsql, null);
int list_cols = dtList.Columns.Count; //字段數(shù)
int list_rows = dtList.Rows.Count; //記錄數(shù)
string listlable = ""; //顯示內容
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.InsertBefore(dec, xmldoc.FirstChild);
XmlElement root = xmldoc.CreateElement("duxion");
xmldoc.AppendChild(root);
//顯示表頭
if (list_rows > 0)
{
XmlElement objtitle = xmldoc.CreateElement("object");
for (int k = 0; k < list_cols; k++)
{
listlable += "<span style='width:" + arrwidth[k] + ";text-align:center;font-weight:bold;color:#000000;padding-top:2px;'>" + arrfield[k] + "</span>";
if (k == arrfield.Length - 1)
break;
}
objtitle.SetAttribute("fullcontent", listlable);
root.AppendChild(objtitle);
}
//顯示表頭
//顯示數(shù)據
for (int i = 0; i < list_rows; i++)
{
listlable = "";
DataRow drList = dtList.Rows[i];
XmlElement obj = xmldoc.CreateElement("object");
for (int j = 0; j < list_cols; j++)
{
obj.SetAttribute(dtList.Columns[j].ColumnName, drList[j].ToString());
if (j <= arrfield.Length - 1)
{
//listlable += "<span style='width:" + arrwidth[j] + "'>" + GetLeftString(drList[j].ToString(), Convert.ToInt16(arrwidth[j])) + "</span>";
listlable += "<span style='width:" + arrwidth[j] + "'>" + formatXmlNode(dtList.Columns[j].DataType.ToString(), drList[j].ToString()) + "</span>";
}
}
obj.SetAttribute("fullcontent", listlable);
root.AppendChild(obj);
}
//顯示數(shù)據
//Response.ContentType = "text/xml;charset=utf-8";
//Response.ContentType = "text/xml;charset=gb2312";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "text/xml";
Response.Clear();
Response.Write(xmldoc.OuterXml);
Response.End();
}
private string formatXmlNode(string field_type, string field_value)
{
string return_value = field_value.Trim();
switch (field_type)
{
case "System.Double":
case "System.Decimal":
return_value = string.Format("{0:0.###}", Convert.ToDouble(return_value));
break;
}
return return_value;
}
}
3.效果圖:

4。 另外的.js以及.css文件見附件
復制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="jscript/autoSuggest.css" type="text/css"/>
<link rel="stylesheet" href="jscript/ac.css" type="text/css"/>
<script language="Javascript" src="jscript/autoSuggest.js"></script>
<script language="Javascript" src="jscript/ac.js"></script>
function ACClient( queryurl, width, objInput, objLable )
{
this.xmldom = getXmlhttp();
this.queryurl = queryurl;
this.prolist = new Array();
this.divwidth = width;
this.objInput = objInput;
this.getEligible = function(ac) {
var eligible = new Array();
if (ac.inputText.length < 1) {
document.getElementById(objInput.name).value = "";
document.getElementById(objLable.name).value = "";
return eligible;
}
var strURL = this.queryurl + escape(ac.inputText) + "&rand=" + Math.random();
this.xmldom.open("GET", strURL, false);
try {
this.xmldom.send();
}
catch (e) {
return;
}
this.prolist = this.xmldom.responseXML.documentElement.selectNodes("/duxion/object");
if (0 == this.prolist.length) {
return eligible;
}
for (var i = 0; i < this.prolist.length; i++) {
if (i > 15)
break;
var node = this.prolist.item(i);
eligible[eligible.length] = node.getAttribute("fullcontent");
}
ac.div.style.width = this.divwidth;
return eligible;
};
this.useSuggestion = function( sel )
{
if( sel>=0 )
{
var node = this.prolist.item(sel);
if(typeof(node.getAttribute( "value" )) != "undefined")
document.getElementById(objInput.name).value = node.getAttribute( "value" );
else
document.getElementById(objInput.name).value = "";
if(typeof(node.getAttribute( "content" )) != "undefined")
document.getElementById(objLable.name).value = node.getAttribute( "content" );
else
document.getElementById(objLable.name).value = "";
if (this.objInput.name == "hidfreq_code") {
document.getElementById("txtfreq_name").value = node.getAttribute("Name");
document.getElementById("hidfreq_code").value = node.getAttribute("Code");
}
}
};
return this;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<%--div 用于顯示下拉查詢部分--%>
<div id="autosuggest" style="width:160px;font-size:12px;"><ul></ul></div>
<div>
<asp:TextBox ID="txtfreq_name" CssClass="needtext" runat="server" Width="100px"></asp:TextBox><asp:HiddenField
ID="hidfreq_code" runat="server" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
new AutoSuggest(document.getElementById('txtfreq_name'),
new ACClient("testXml.aspx?tag=yp_frequency&value=", "200px", document.getElementById('hidfreq_code'), document.getElementById('txtfreq_name')));
</script>
2。 供查詢的xml的testxml.aspx.cs文件
復制代碼 代碼如下:
public partial class testXml : BaseForm
{
protected void Page_Load(object sender, EventArgs e)
{
ListXmlData();
}
private string[] arrwidth;
private string[] arrfield;
public string fieldlist = "區(qū)域Id,區(qū)域名,區(qū)域編碼,區(qū)域層次";
public string coloumwidth = "60,10,20,30";
//顯示xml格式數(shù)據
private void ListXmlData()
{
//各字段的寬度
string strwhere="";
if (Request["value"] != null)
{
strwhere = string.Format(" and Code like '{0}%'", Request["value"].ToString());
}
arrfield = fieldlist.Split(',');
arrwidth = coloumwidth.Split(',');
DataTable dtList = new Districts().Search("1=1" + strwhere);// dis // SqlHelper.GetTable(subsys_db, CommandType.Text, strsql, null);
int list_cols = dtList.Columns.Count; //字段數(shù)
int list_rows = dtList.Rows.Count; //記錄數(shù)
string listlable = ""; //顯示內容
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.InsertBefore(dec, xmldoc.FirstChild);
XmlElement root = xmldoc.CreateElement("duxion");
xmldoc.AppendChild(root);
//顯示表頭
if (list_rows > 0)
{
XmlElement objtitle = xmldoc.CreateElement("object");
for (int k = 0; k < list_cols; k++)
{
listlable += "<span style='width:" + arrwidth[k] + ";text-align:center;font-weight:bold;color:#000000;padding-top:2px;'>" + arrfield[k] + "</span>";
if (k == arrfield.Length - 1)
break;
}
objtitle.SetAttribute("fullcontent", listlable);
root.AppendChild(objtitle);
}
//顯示表頭
//顯示數(shù)據
for (int i = 0; i < list_rows; i++)
{
listlable = "";
DataRow drList = dtList.Rows[i];
XmlElement obj = xmldoc.CreateElement("object");
for (int j = 0; j < list_cols; j++)
{
obj.SetAttribute(dtList.Columns[j].ColumnName, drList[j].ToString());
if (j <= arrfield.Length - 1)
{
//listlable += "<span style='width:" + arrwidth[j] + "'>" + GetLeftString(drList[j].ToString(), Convert.ToInt16(arrwidth[j])) + "</span>";
listlable += "<span style='width:" + arrwidth[j] + "'>" + formatXmlNode(dtList.Columns[j].DataType.ToString(), drList[j].ToString()) + "</span>";
}
}
obj.SetAttribute("fullcontent", listlable);
root.AppendChild(obj);
}
//顯示數(shù)據
//Response.ContentType = "text/xml;charset=utf-8";
//Response.ContentType = "text/xml;charset=gb2312";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "text/xml";
Response.Clear();
Response.Write(xmldoc.OuterXml);
Response.End();
}
private string formatXmlNode(string field_type, string field_value)
{
string return_value = field_value.Trim();
switch (field_type)
{
case "System.Double":
case "System.Decimal":
return_value = string.Format("{0:0.###}", Convert.ToDouble(return_value));
break;
}
return return_value;
}
}
3.效果圖:

4。 另外的.js以及.css文件見附件
您可能感興趣的文章:
- asp.net+ajaxfileupload.js 實現(xiàn)文件異步上傳代碼分享
- asp.net 使用js分頁實現(xiàn)異步加載數(shù)據
- asp.net jquery+ajax異步刷新實現(xiàn)示例
- ASP.NET之自定義異步HTTP處理程序(圖文教程)
- jquery異步調用頁面后臺方法‏(asp.net)
- JQuery FlexiGrid的asp.net完美解決方案 dotNetFlexGrid-.Net原生的異步表格控件
- asp.net下實現(xiàn)支持文件分塊多點異步上傳的 Web Services
- ASP.NET2.0中數(shù)據源控件之異步數(shù)據訪問
- asp.net實現(xiàn)數(shù)據從DataTable導入到Excel文件并創(chuàng)建表的方法
- ASP.NET DataTable去掉重復行的2種方法
- ASP.NET中DataTable與DataSet之間的轉換示例
- ASP.NET怎么操作DataTable實例應用
- asp.net 讀取Excel數(shù)據到DataTable的代碼
- asp.net異步獲取datatable并顯示的實現(xiàn)方法
相關文章
ZeroClipboard插件實現(xiàn)多瀏覽器復制功能(支持firefox、chrome、ie6)
Zero Clipboard 利用透明的Flash讓其漂浮在復制按鈕之上,這樣其實點擊的不是按鈕而是Flash ,這樣將需要的內容傳入Flash,再通過Flash的復制功能把傳入的內容復制到剪貼板2014-08-08JavaScript中的立即執(zhí)行函數(shù)表達式介紹
這篇文章主要介紹了JavaScript中的立即執(zhí)行函數(shù)表達式介紹,本文著重講解了什么是立即調用函數(shù)表達式,需要的朋友可以參考下2015-03-03微信小程序使用scroll-view標簽實現(xiàn)自動滑動到底部功能的實例代碼
本文通過實例代碼給大家介紹了微信小程序使用scroll-view標簽實現(xiàn)自動滑動到底部功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-11-11JavaScript原型對象、構造函數(shù)和實例對象功能與用法詳解
這篇文章主要介紹了JavaScript原型對象、構造函數(shù)和實例對象功能與用法,結合實例形式分析了javascript面向對象相關原型對象、構造函數(shù)及實例對象的概念、功能與相關使用技巧,需要的朋友可以參考下2018-08-08