asp.net和ajax實現(xiàn)智能搜索功能代碼
更新時間:2010年03月04日 19:44:26 作者:
近來一直在開發(fā)股票模擬系統(tǒng),終于告一段落了,回想起來感慨很多。突然想應(yīng)該做點總結(jié)了,想來想去還是覺得通過寫點日志來把相關(guān)的知識點記錄下來,下面就我在項目中經(jīng)常用到的動態(tài)提示搜索選項功能的實現(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}
}
最后實現(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}
}
最后實現(xiàn)的效果如下:

相關(guān)文章
基于ASP.NET MVC的ABP框架入門學(xué)習(xí)教程
ABP是基于Windows系統(tǒng)上.NET Framework環(huán)境的Web開發(fā)框架,這里我們基于.NET的Visual Studio開發(fā)環(huán)境,來共同進(jìn)入基于ASP.NET MVC的ABP框架入門學(xué)習(xí)教程2016-06-06swagger上傳文件并支持jwt認(rèn)證的實現(xiàn)方法
今天通過本文給大家分享swagger上傳文件并支持jwt認(rèn)證的實現(xiàn)方法,文中提到了安裝方法及實現(xiàn)代碼,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05詳解Spring Boot 中使用 Java API 調(diào)用 lucene
這篇文章主要介紹了詳解Spring Boot 中使用 Java API 調(diào)用 lucene,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04解決Visual Studio 2012 Update 4 RC啟動調(diào)試失敗的方案
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2012 Update 4 RC啟動調(diào)試失敗的解決方案,感興趣的小伙伴們可以參考一下2016-05-05