Google suggestドロップダウンメニュー実装コード(asp.netバージョン)
9442 ワード
1.suggest.jsはsuggestとして保存する.jsは必要なページで引用すればいいです.
2.フロントページ
3.バックグラウンド ページ
// xmlHttp
var xmlHttp;
var k=-1;
try
{
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
var success = false;
for(var i=0;i{
try {
xmlHttp = new ActiveXObject(XMLHTTP_IDS[i]);
success = true;
} catch (e) {}
}
if (!success) {
throw new Error('Unable to create XMLHttpRequest.');
}
}
//
function initVars() {
inputField = document.getElementById("FrmChangshang");
nameTable = document.getElementById("name_table");
completeDiv = document.getElementById("popup");
nameTableBody = document.getElementById("name_table_body");
document.getElementById("popup").style.display="block";
}
//ajax
function findNames(event) {
var keyc=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which;
if(keyc!=40 && keyc!=38)
{
if (inputField.value.length > 0)
{
var url = "SearchCompany.aspx?cname=" + inputField.value +"&rnd="+Math.random().toString();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
else
{
clearNames();
}
}
}
//ajax
function callback()
{
var names;
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200)
{
try
{
names=xmlHttp.responseXML.getElementsByTagName("name");
}
catch(e)
{
completeDiv.style.display="none";
clearNames();
}
k=-1;//
setNames(names);
}
else if (xmlHttp.status == 204)
{
clearNames();
}
}
}
//
function divsOffset()
{
var inputWidth=inputField.offsetWidth;
var inputHeight=inputField.offsetHeight;
var inputTop=inputField.offsetTop;
var inputLeft=inputField.offsetLeft;
nameTable.style.width=inputWidth+"px";
completeDiv.style.width=inputWidth+"px";
completeDiv.style.top=(getTop(inputField)+inputHeight)+"px";
completeDiv.style.left=getLeft(inputField)+"px";
completeDiv.style.border = "#817f82 1px solid";
completeDiv.style.position = "absolute";
//
function getTop(e){
var offset=e.offsetTop;
if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
return offset;
}
//
function getLeft(e){
var offset=e.offsetLeft;
if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
return offset;
}
//alert(inputWidth+"----"+inputwidth+"----"+inputtopleft+"----"+inputtop);
}
//
function setNames(the_names) {
clearNames();
divsOffset();
for(var i=0;i{
var nextNode=the_names[i].firstChild.nodeValue;
var row =document.createElement("tr");
var cell =document.createElement("td");
cell.onmouseout = function() {this.className="mouseOut";};
cell.onmouseover =(function(tdi) {return function(){k=tdi;setStyle(k);}})(i);
cell.setAttribute("bgcolor","#FFFAFA");
cell.setAttribute("border","0");
cell.setAttribute("style","font-size:15px");
cell.onclick = function() { populateName(this); };
// nextNode td
var txtName = document.createTextNode(nextNode);
cell.appendChild(txtName);
row.appendChild(cell);
nameTableBody.appendChild(row);
}
}
// , inputField
function populateName(cell) {
// web ,cell---->td
inputField.value = cell.firstChild.nodeValue;
clearNames();
}
//
function setStyle(num)
{
var tBody = nameTableBody.getElementsByTagName("td");
for(var i=0;i{
if(i>=0 && i{
tBody[i].className="mouseOver";
}
else
{
tBody[i].className="mouseOut";
}
}
}
//
function updown(e)
{
var tBody = nameTableBody.getElementsByTagName("td");
var keyc=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which;
if(tBody==null)
return;
if(keyc==40)
{
k++;
if(k>=tBody.length)
{
k=0;
}
inputField.value=tBody[k].firstChild.nodeValue;
}
else if(keyc==38)
{
k--;
if(k<=-1)
{
k=tBody.length-1;
}
inputField.value=tBody[k].firstChild.nodeValue;
}
setStyle(k);
}
//
function clearNames() {
var ind = nameTableBody.childNodes.length;
var completeDiv = document.getElementById("popup");
for (var i = ind - 1; i >= 0 ; i--) {
nameTableBody.removeChild(nameTableBody.childNodes[i]);
}
completeDiv.style.border = "none";
}
2.フロントページ
3.バックグラウンド ページ
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;
using CaiHong.DBUtility;
public partial class member_SearchCompany : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 。 , SqlHelper, , SqlHelper , 。 。
string strconn = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
string keyword = Request.QueryString["cname"];
string sql = "select Member_TrueName from users where Member_TrueName like '%" + @keyword + "%' and Member_ShenFen=1 order by id desc";
SqlParameter spr = new SqlParameter("@keyword", SqlDbType.NVarChar, 50);
spr.Value = keyword;
Response.ContentType = "text/xml";
Response.Write("");
Response.Write("");
using (SqlConnection sqlconn = new SqlConnection(strconn))
{
sqlconn.Open();
SqlDataReader dre = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, sql);
if (dre.HasRows)
{
while (dre.Read())
{
Response.Write("");
Response.Write("" + dre.GetString(0) + " ");
Response.Write(" ");
}
}
else
{
Response.Write("");
Response.Write("IsKong ");
Response.Write(" ");
}
Response.Write(" ");
dre.Close();
}
}
}