智能搜索

备份下,以便以后用。。。

GoogleSuggest.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoogleSuggest.aspx.cs" Inherits="Admin_GoogleSuggest" %>

<!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></title>


</head>
<body>
<form id="form1" action="">
<input type="text" id="txtSearch" name="txtSearch" onkeyup="searchSuggest();" size="20" class="input_out" onfocus="this.className='input_on';this.οnmοuseοut=''" onblur="this.className='input_off';this.οnmοuseοut=function(){this.className='input_out'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" /><br />
<div id="search_suggest" style="display:none"></div>
</form>
</body>
</html>

js文件:

ContractedBlock.gif ExpandedBlockStart.gif View Code
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
var obj;
if(window.XMLHttpRequest) { //Mozilla 浏览器
obj = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
obj = new ActiveXObject( "Msxml2.XMLHTTP");
} catch(e){
try{
obj = new ActiveXObject( "Microsoft.XMLHTTP");
} catch (e) {}
}
}
return obj;
}
//当输入框的内容变化时,调用该函数
function searchSuggest() {
var inputField = document.getElementById( "txtSearch");
var suggestText = document.getElementById( "search_suggest");

if (inputField.value.length > 0) {
var o=createXMLHttpRequest();

var url = "Search.aspx?searchText=" + escape(inputField.value);
o.open("GET", url, true);
o.onreadystatechange = function(){
if(o.readyState == 4){
if(o.status == 200){
var sourceText = o.responseText.split( "\n");
if(sourceText.length >1){
suggestText.style.display= "";
suggestText.innerHTML = "";
for(var i=0;i <sourceText.length-1;i++) {
var s=' <div οnmοuseοver="javascript:suggestOver(this);"';
s+=' οnmοuseοut= "javascript:suggestOut(this);" ';
s+=' οnclick= "javascript:setSearch(this.innerHTML);" ';
s+=' class= "suggest_link">' +sourceText[i]+'</div>';
suggestText.innerHTML += s;
}
}
else{
suggestText.style.display= "none";
}
}
}
};//指定响应函数
o.send(null); // 发送请求
}
else {
suggestText.style.display= "none";
}
}

function suggestOver(div_value){
div_value.className = "suggest_link_over";
}

function suggestOut(div_value){
div_value.className = "suggest_link";
}

function setSearch(obj){
document.getElementById("txtSearch").value = obj;
var div = document.getElementById( "search_suggest");
div.innerHTML = "";
div.style.display= "none";
}

function tbblur(){
var div = document.getElementById("search_suggest");
//div.innerHTML = "";
div.style.display= "none";
}

CSS文件:

ContractedBlock.gif ExpandedBlockStart.gif View Code
.suggest_link
{
background-color
: #FFFFFF;
padding
: 2px 6px 2px 6px;
}
.suggest_link_over
{
background-color
: #E8F2FE;
padding
: 2px 6px 2px 6px;
}
#search_suggest
{
position
: absolute;
background-color
: #FFFFFF;
text-align
: left;
border
: 1px solid #000000;
width
:160px;
}
/*input*/
.input_on
{
padding
:2px 8px 0pt 3px;
height
:18px;
border
:1px solid #999;
background-color
:#FFFFCC;
}
.input_off
{
padding
:2px 8px 0pt 3px;
height
:18px;
border
:1px solid #CCC;
background-color
:#FFF;
}
.input_move
{
padding
:2px 8px 0pt 3px;
height
:18px;
border
:1px solid #999;
background-color
:#FFFFCC;
}
.input_out
{
/*height:16px;默认高度*/
padding
:2px 8px 0pt 3px;
height
:18px;
border
:1px solid #CCC;
background-color
:#FFF;

处理页:Search.aspx

后台:

ContractedBlock.gif ExpandedBlockStart.gif View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["searchText"] != null)
{
if (Request.QueryString["searchText"].ToString().Trim().Length > 0)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection("Server=127.0.0.1;DataBase=CRM;User Id=sa;pwd=1234"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format(
"Select distinct top 10 customername From customertest Where firstletter like '%{0}%'",
TY.CRM.Comm.StrToPinyin.GetChineseSpell((Request.QueryString["searchText"]+"")).Trim());
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
string returnText = "";
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
returnText += dt.Rows[i][0].ToString() + "\n";
}
}

Response.Write(returnText);
}
}
}
}

 


 



 

转载于:https://www.cnblogs.com/niezy/archive/2011/10/11/2207124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值