Google suggest Drop-down menu implementation code (ASP.net version) _ Practical Tips

Source: Internet
Author: User
1.suggest.js copy code saved as suggest.js on the page you need to quote on the line
Copy Code code as follows:

Building cross-Browser XMLHTTP object references
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<xmlhttp_ids.length&&!success;i++)
{
try {
XmlHttp = new ActiveXObject (xmlhttp_ids[i]);
Success = true;
catch (e) {}
}
if (!success) {
throw new Error (' Unable to create XMLHttpRequest. ');
}
}
Get the reference variable used by the page
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 Foreground Trigger function
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 callback function
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;//again from the first item down to select
Setnames (names);
}
else if (xmlhttp.status = 204)
{
Clearnames ();
}
}
}
Pop-up Layer Position determination function
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";
Gets the absolute ordinate of an element
function GetTop (e) {
var offset=e.offsettop;
if (e.offsetparent!=null) offset+=gettop (e.offsetparent);
return offset;
}
Get the horizontal axis of an element
function GetLeft (e) {
var offset=e.offsetleft;
if (e.offsetparent!=null) offset+=getleft (e.offsetparent);
return offset;
}
Alert (inputwidth+ "----" +inputwidth+ "----" +inputtopleft+ "----" +inputtop);
}
Add returned data to a table function
function Setnames (the_names) {
Clearnames ();
Divsoffset ();
for (Var i=0;i<the_names.length;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);
Add NextNode to TD
var txtname = document.createTextNode (NextNode);
Cell.appendchild (txtname);
Row.appendchild (cell);
Nametablebody.appendchild (row);
}
}
Click to select a piece of data to add to Inputfield
function Populatename (cell) {
Populate data to Web pages, cell----&GT;TD objects
Inputfield.value = Cell.firstChild.nodeValue;
Clearnames ();
}
Set style
function SetStyle (num)
{
var tbody = nametablebody.getelementsbytagname ("TD");
for (Var i=0;i<tbody.length;i++)
{
if (i>=0 && i<tbody.length&&i==num)
{
Tbody[i].classname= "MouseOver";
}
Else
{
Tbody[i].classname= "Mouseout";
}
}
}
Get keyboard up and down keys
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);
}
Clear List Array
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. Front Page
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Searchcom.aspx.cs" inherits= "member_searchcom"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<style type= "Text/css" >
<!--
. mouseout
{
font-size:15px;
Background: #ffffff;
Color: #000000;
}
. mouseOver
{
font-size:15px;
Background: #3366cc;
Color: #ffffff;
Cursor:pointer;
}
-->
</style>
<title> Untitled Page </title>
<script language= "javascript" src= ". /js/suggest.js "type=" Text/javascript "></script>

<body onload= "Initvars ();" >
<form id= "Form1" runat= "Server" >
<div>
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
&LT;TD >
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td><input id= "Frmchangshang" name= "Frmchangshang" type= "text" style= "width:320px"; "Maxlength=" onkeyup= "Findnames" (event), "onkeydown=" UpDown (event), "onclick=" Findnames (event); />
<div id= "Popup" >
<table id= "name_table" bgcolor= "#FFFAFA" border= "0" cellspacing= "0" cellpadding= "3px" >
<tbody id= "Name_table_body" ></tbody>
</table>
</div>
</td>
</tr>
</table>
<br/>
<br/>
<br/>
</div>
</form>
</body>


3. Background Processing page
Copy Code code as follows:

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)
{
Get your own database connection connection. Database calls, using the SqlHelper, to use the following code, download sqlhelper to your own definition folder, and then add a reference. No, please write your own database access code.
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 ("<?xml version=\" 1.0\ "encoding=\" gb2312\ "?>");
Response.Write ("<response>");
using (SqlConnection sqlconn = new SqlConnection (strconn))
{
Sqlconn. Open ();
SqlDataReader dre = Sqlhelper.executereader (sqlconn, CommandType.Text, SQL);
if (Dre. HasRows)
{
while (Dre. Read ())
{
Response.Write ("<content>");
Response.Write ("<name>" + dre). GetString (0) + "</name>");
Response.Write ("</content>");
}
}
Else
{
Response.Write ("<content>");
Response.Write ("<name>IsKong</name>");
Response.Write ("</content>");
}
Response.Write ("</response>");
Dre. Close ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.