asp.net automatic completion of text box input __.net

Source: Internet
Author: User

This paper is based on http://hi.baidu.com/kb81/blog/item/6f7e040848be85d162d98634.html related content

Default.aspx Code
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<script type= "Text/javascript" language= "JavaScript" >
var xmlhttpreq;
var Completediv;
var Inputfield;
var completetable;
var completebody;
Creating XMLHttpRequest Objects
function Createxmlhttprequest () {
if (window. XMLHttpRequest) {//mozilla browser
Xmlhttpreq = new XMLHttpRequest ();
}
else if (window. ActiveXObject) {//IE browser
try {
Xmlhttpreq = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Xmlhttpreq = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {}
}
}
}

Send matching request function
function Findnames () {
Inputfield = document.getElementById ("names");
completetable = document.getElementById ("complete_table");
Completediv = document.getElementById ("popup");
Completebody = document.getElementById ("Complete_body");
if (InputField.value.length > 0) {
Createxmlhttprequest ();
var url = "default2.aspx?names=" + Escape (inputfield.value); //
Xmlhttpreq.open ("Get", url, True);
Xmlhttpreq.onreadystatechange = Processmatchresponse; Specifying response functions
Xmlhttpreq.send (NULL); Send Request
} else {
Clearnames ();
}
}

Processing return matching information function
function Processmatchresponse () {
if (xmlhttpreq.readystate = = 4) {//Judge object state
if (Xmlhttpreq.status = = 200) {//information has been successfully returned to start processing information
Setnames (XMLHttpReq.responseXML.getElementsByTagName ("res"));
} else {//page is not normal
Window.alert ("The page you requested has an exception.) ");
}
}
}

Generate matching rows with input
function Setnames (names) {

Clearnames ();
var size = Names.length;

Setoffsets ();
var row, cell, Txtnode;
for (var i = 0; i < size; i++) {
var nextnode = Names[i].firstchild.data;
row = Document.createelement ("tr");
Cell = document.createelement ("TD");
Cell.onmouseout = function () {
This.classname = ' mouseOver ';
};
Cell.onmouseover = function () {
This.classname = ' mouseout ';
};
Cell.setattribute ("bgcolor", "#ffddcc");
Cell.setattribute ("Border", "0");
Cell.onclick = function () {
Completefield (this);
};
Txtnode = document.createTextNode (NextNode);
Cell.appendchild (Txtnode);
Row.appendchild (cell);
Completebody.appendchild (row);
}
}

Set Display location
function Setoffsets () {
CompleteTable.style.width = "Auto"; Automatically expand or shrink the width of the automatically displayed balloon
var left = Calculateoffset (Inputfield, "offsetleft");
var top = Calculateoffset (Inputfield, "offsettop") + inputfield.offsetheight;
CompleteDiv.style.border = "Black 1px solid";
CompleteDiv.style.left = left + "px";
CompleteDiv.style.top = top + "px";
}

Calculate Display Position
function Calculateoffset (field, attr) {
var offset = 0;
while (field) {
Offset + + field[attr];
field = Field.offsetparent;
}
return offset;
}

Fill in the Input box
function Completefield (cell) {
Inputfield.value = Cell.firstChild.nodeValue;
Clearnames ();
}

Clear AutoComplete Lines
function Clearnames () {
var ind = CompleteBody.childNodes.length;
for (var i = ind-1 i >= 0; i--) {
Completebody.removechild (Completebody.childnodes[i]);
}
CompleteDiv.style.border = "None";
}

</script>

<body>
<form id= "Form1" runat= "Server" >

Dictionary
<p style= "Text-align:center;" >
<label for= "Names" >
Search:
<asp:textbox id= "Names" runat= "Server" ></asp:TextBox></label>
<asp:button id= "Button1" runat= "server" text= "Go"/>
</p>
<div style

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.