asp.net和ajax實(shí)現(xiàn)智能搜索功能代碼
更新時間:2010年03月04日 19:44:26 作者:
近來一直在開發(fā)股票模擬系統(tǒng),終于告一段落了,回想起來感慨很多。突然想應(yīng)該做點(diǎn)總結(jié)了,想來想去還是覺得通過寫點(diǎn)日志來把相關(guān)的知識點(diǎn)記錄下來,下面就我在項(xiàng)目中經(jīng)常用到的動態(tài)提示搜索選項(xiàng)功能的實(shí)現(xiàn)。
第一步,先做好搜索頁面
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>無標(biāo)題頁</title>
<script language=javascript src=JScript.js type="text/javascript" ></script>
<style>
#result{
position:absolute;
width:150px;
height:auto;
margin:0px;
z-index:1;
font-size:14px;
border: 1px dashed #ccccc4;
display:none;
}
#result .firstHang{
background-color:#DDDDDD;
height:15px;
padding-top:5px;
}
#result b{
width:61px;
float:left;
}
#result nobr{
width:61px;
float:left;
}
#result .otherHang{
background-color:#FFFFFF;
height:15px;
padding-top:5px;
}
#content{
margin-left:0px;
padding-left:0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align=center style="padding-top:100px">
<input id="searchTxt" onkeyUp="startRequest(this.value)" /> <!-- 輸入框 -->
</div>
<div id="result" align="center"> <!-- 下拉搜索框 -->
<div class="firstHang">
<b>搜索</b><b>標(biāo)題</b>
</div>
<div id="stockListDiv"></div>
</div>
</form>
</body>
</html>
<script language="javascript">
var obj=document.getElementById("result");
var rela=document.getElementById("searchTxt");
SetDivLocation(obj,rela);
function SetDivLocation(obj,rela) //設(shè)置下拉搜索框與輸入框的相對位置
{
var x,y;
var oRect=rela.getBoundingClientRect(); //獲得輸入框的位置
x=oRect.left;
y=oRect.top;
obj.style.left=x+"px"; //這里要加上px,否則在fiexfox就會失效
obj.style.top=y+rela.offsetHeight+"px";
}
</script>
第二步,添加返回搜索結(jié)果的頁面,該頁面由于不用在客戶端顯示,所以就不用做界面。
Imports System.Text
Partial Class Search
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim searchContent As String = Request("content").ToString '獲取搜索內(nèi)容
Dim searchResult As New StringBuilder
If IsNumeric(searchContent) Then '判斷是否為數(shù)字,輸入不同的內(nèi)容
searchResult.Append("<div class='otherHang'><nobr>11</nobr><nobr>11</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>22</nobr><nobr>22</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>22</nobr><nobr>22</nobr></div>")
Else
searchResult.Append("<div class='otherHang'><nobr>aa</nobr><nobr>aa</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>bb</nobr><nobr>bb</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>cc</nobr><nobr>cc</nobr></div>")
End If
Response.Write(searchResult.ToString) '向客戶端發(fā)送結(jié)果
Response.End() '關(guān)閉客戶端輸出流
End Sub
End Class
第三步就是最關(guān)鍵的一步了
// JScript 文件
var xmlHttp;
function cratexmlHttpRequest()
{
//判斷是否為IE瀏覽器
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new window.XMLHttpRequest();
}
}
//啟動對頁面的請求
function startRequest(content)
{
cratexmlHttpRequest();
//設(shè)置請求狀態(tài)變化調(diào)用的方法
xmlHttp.onreadystatechange=handleState;
//建立對服務(wù)器的調(diào)用
var url="Search.aspx?content="+escape(content); '發(fā)送頁面url
xmlHttp.open("get",url,true);
xmlHttp.send(null);
}
function handleState()
{
try{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data=xmlHttp.responseText; '得到搜索結(jié)果
var result=document.getElementById("result");
var stockListDiv=document.getElementById("stockListDiv");
if(data=="") '如果搜索結(jié)果為空,不顯示下拉框
{
result.style.display="none";
stockListDiv.innerHTML="";
}
else
{
stockListDiv.innerHTML=data; '顯示下拉框
result.style.display="block";
}
}
}
}
catch(error)
{error.message}
}
最后實(shí)現(xiàn)的效果如下:
復(fù)制代碼 代碼如下:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>無標(biāo)題頁</title>
<script language=javascript src=JScript.js type="text/javascript" ></script>
<style>
#result{
position:absolute;
width:150px;
height:auto;
margin:0px;
z-index:1;
font-size:14px;
border: 1px dashed #ccccc4;
display:none;
}
#result .firstHang{
background-color:#DDDDDD;
height:15px;
padding-top:5px;
}
#result b{
width:61px;
float:left;
}
#result nobr{
width:61px;
float:left;
}
#result .otherHang{
background-color:#FFFFFF;
height:15px;
padding-top:5px;
}
#content{
margin-left:0px;
padding-left:0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align=center style="padding-top:100px">
<input id="searchTxt" onkeyUp="startRequest(this.value)" /> <!-- 輸入框 -->
</div>
<div id="result" align="center"> <!-- 下拉搜索框 -->
<div class="firstHang">
<b>搜索</b><b>標(biāo)題</b>
</div>
<div id="stockListDiv"></div>
</div>
</form>
</body>
</html>
<script language="javascript">
var obj=document.getElementById("result");
var rela=document.getElementById("searchTxt");
SetDivLocation(obj,rela);
function SetDivLocation(obj,rela) //設(shè)置下拉搜索框與輸入框的相對位置
{
var x,y;
var oRect=rela.getBoundingClientRect(); //獲得輸入框的位置
x=oRect.left;
y=oRect.top;
obj.style.left=x+"px"; //這里要加上px,否則在fiexfox就會失效
obj.style.top=y+rela.offsetHeight+"px";
}
</script>
第二步,添加返回搜索結(jié)果的頁面,該頁面由于不用在客戶端顯示,所以就不用做界面。
復(fù)制代碼 代碼如下:
Imports System.Text
Partial Class Search
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim searchContent As String = Request("content").ToString '獲取搜索內(nèi)容
Dim searchResult As New StringBuilder
If IsNumeric(searchContent) Then '判斷是否為數(shù)字,輸入不同的內(nèi)容
searchResult.Append("<div class='otherHang'><nobr>11</nobr><nobr>11</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>22</nobr><nobr>22</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>22</nobr><nobr>22</nobr></div>")
Else
searchResult.Append("<div class='otherHang'><nobr>aa</nobr><nobr>aa</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>bb</nobr><nobr>bb</nobr></div>")
searchResult.Append("<div class='otherHang'><nobr>cc</nobr><nobr>cc</nobr></div>")
End If
Response.Write(searchResult.ToString) '向客戶端發(fā)送結(jié)果
Response.End() '關(guān)閉客戶端輸出流
End Sub
End Class
第三步就是最關(guān)鍵的一步了
復(fù)制代碼 代碼如下:
// JScript 文件
var xmlHttp;
function cratexmlHttpRequest()
{
//判斷是否為IE瀏覽器
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new window.XMLHttpRequest();
}
}
//啟動對頁面的請求
function startRequest(content)
{
cratexmlHttpRequest();
//設(shè)置請求狀態(tài)變化調(diào)用的方法
xmlHttp.onreadystatechange=handleState;
//建立對服務(wù)器的調(diào)用
var url="Search.aspx?content="+escape(content); '發(fā)送頁面url
xmlHttp.open("get",url,true);
xmlHttp.send(null);
}
function handleState()
{
try{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data=xmlHttp.responseText; '得到搜索結(jié)果
var result=document.getElementById("result");
var stockListDiv=document.getElementById("stockListDiv");
if(data=="") '如果搜索結(jié)果為空,不顯示下拉框
{
result.style.display="none";
stockListDiv.innerHTML="";
}
else
{
stockListDiv.innerHTML=data; '顯示下拉框
result.style.display="block";
}
}
}
}
catch(error)
{error.message}
}
最后實(shí)現(xiàn)的效果如下:
相關(guān)文章
ASP.NET Cookie 操作實(shí)現(xiàn)
本節(jié)中的主題描述如何在 ASP.NET Web 應(yīng)用程序中創(chuàng)建 Cookie。Cookie 是一些小的文本文件,服務(wù)器和瀏覽器在收到每個頁請求時交換它們,您還可以使用這些小文本文件來存儲幫助針對每個用戶自定義您的應(yīng)用程序的信息。2009-11-11
記Asp.Net Core Swagger使用并帶域接口處理的方法
這篇文章主要介紹了記Asp.Net Core Swagger使用并帶域接口處理的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
VS2010發(fā)布Web網(wǎng)站技術(shù)攻略
本篇文章主要包含了完整的發(fā)布網(wǎng)站步驟、發(fā)布網(wǎng)站過程中可能遇到的問題,以及配套的解決方法,相信感興趣的朋友一定會喜歡這篇文章的2015-07-07
ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法
本篇文章主要給大家詳細(xì)分析了ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法以及相關(guān)的實(shí)例代碼,有需要的朋友參考下吧。2018-01-01
.Net獲取URL中文參數(shù)值的亂碼問題解決方法總結(jié)
這篇文章主要介紹了.Net獲取URL中文參數(shù)值的亂碼問題解決方法,總結(jié)分析了針對URL參數(shù)傳遞中出現(xiàn)的亂碼問題與相應(yīng)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08
asp.net 驗(yàn)證碼的簡單制作(vb.net+C#)
asp.net中實(shí)現(xiàn)簡單驗(yàn)證碼的方法,需要的朋友可以參考下2012-05-05
使用Aspose.Cells組件生成Excel文件實(shí)例
這篇文章主要介紹了使用Aspose.Cells組件生成Excel文件的方法,大家參考使用吧2013-11-11

