首页 新闻 搜索 专区 学院

求高手给一个详细的.net自动补全的代码类似百度的那种

0
悬赏园豆:50 [已解决问题] 解决于 2011-03-30 09:54
如题我在网上试了很多可是就是不好用啊!
比如这个: http://topic.csdn.net/u/20101222/12/a8638e0b-f317-444a-bc8e-45ebfea2ba61.html
求高手给我一个简单点的代码谢谢了,大家帮一下忙吧!
最好是不用装ajax扩展包的那种,用jquery写的最好了!谢谢了!
唐学阳的主页 唐学阳 | 初学一级 | 园豆:150
提问于:2011-03-28 17:27
< >
分享
最佳答案
0

1,search.aspx(显示页面)

<!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>
<title>输入框信息提示(数据库)</title>

<script language="javascript" type="text/javascript">
//创建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 = "Server.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 onmouseover="javascript:suggestOver(this);"';
s+=' onmouseout= "javascript:suggestOut(this);" ';
s+=' onclick= "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";
}
</script>

<style type="text/css" media="screen">
body
{
font: 11px arial;
}
.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;
}
/*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;
}
</style>

</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.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_out'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" /><br />
<div id="search_suggest" ></div>
</form>
</body>
</html>

2.1,Server.aspx(数据提供页,此页面中删除其它多余代码,只留最上面一行。如果这里改了名请记得显示页面的调用处也要改哈-_-)

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

2.2,Server.aspx.cs(数据提供页CS代码)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Server : 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=mypc007;DataBase=sql2005_test1;User Id=sa;pwd=123"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format(
"Select distinct top 10 country From iptable Where country like '{0}%'",
Request.QueryString[ "searchText"]);
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);
}
}
}
}

    不是高手,最近也在做,这是网上的,不是jquery写的,是javascript,可以参考一下。

收获园豆:50
Zenic | 菜鸟二级 |园豆:244 | 2011-03-28 17:52
很好用,不过样式有点不兼容!还是谢谢你了!
唐学阳 | 园豆:150 (初学一级) | 2011-03-30 09:53
其他回答(2)
0

推荐Jquery插件autocomplete

peter cheng | 园豆:431 (菜鸟二级) | 2011-03-29 09:19
0

ajax 的autocomplete

程序新青年 | 园豆:841 (小虾三级) | 2011-03-29 09:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册