Basic Ajax tutorial (6) _ tips for creating a tool

Source: Internet
Author: User

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

<Html> <Title> Ajax Tool Tip </title>
<Script type = "text/javascript">
Var xmlHttp;
Var dataDiv;
Var dataTable;
Var dataTableBody;
Var offsetEl;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}

Function initVars (){
DataTableBody = document. getElementByIdx_x ("courseDataBody ");
DataTable = document. getElementByIdx_x ("courseData ");
DataDiv = document. getElementByIdx_x ("popup ");
}

Function getCourseData (element ){
InitVars ();
CreateXMLHttpRequest ();
OffsetEl = element;
Var url = "Handler. ashx? Key = "+ escape (element. id );
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = callback;
XmlHttp. send (null );
}
 
Function callback (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
SetData (xmlHttp. responseXML );
}
}
}

Function setData (courseData ){
ClearData ();
SetOffsets ();
Var length = courseData. getElementsByTagName_r ("length") [0]. firstChild. data;
Var par = courseData. getElementsByTagName_r ("par") [0]. firstChild. data;
Var row, row2;
Var parData = "Par:" + par
Var lengthData = "Length:" + length;
Row = createRow (parData );
Row2 = createRow (lengthData );
DataTableBody. appendChild_xss (row );
DataTableBody. appendChild_xss (row2 );
}
 
Function createRow (data ){
Var row, cell, txtNode;
Row = document. createElement_x ("tr ");
Cell = document. createElement_x ("td ");
Cell. setAttribute ("bgcolor", "# FFFAFA ");
Cell. setAttribute ("border", "0 ");
TxtNode = document. createTextNode (data );
Cell. appendChild_xss (txtNode );
Row. appendChild_xss (cell );
Return row;
}

Function setOffsets (){
Var end = offsetEl. offsetWidth;
Var top = calculateOffsetTop (offsetEl );
DataDiv. style. border = "black 1px solid ";
DataDiv. style. left = end + 15 + "px ";
DataDiv. style. top = top + "px ";
}

Function calculateOffsetTop (field ){
Return calculateOffset (field, "offsetTop ");
}

Function calculateOffset (field, attr ){
Var offset = 0;
While (field ){
Offset + = field [attr];
Field = field. offsetParent;
}
Return offset;
}
Function clearData (){
Var ind = dataTableBody. childNodes. length;
For (var I = ind-1; I> = 0; I --){
DataTableBody. removeChild (dataTableBody. childNodes [I]);
}
DataDiv. style. border = "none ";
} </Script>

<Body>
<H1> Ajax Tool Tip Example <H3> Golf Courses <Table id = "courses" bgcolor = "# FFFAFA" border = "1" cellspacing = "0" cellpadding = "2"/>
<Tbody>
<Tr> <td id = "1" onmouseover = "getCourseData (this);" onmouseout = "clearData ();"> Augusta National </td> </tr>
<Tr> <td id = "2" onmouseover = "getCourseData (this);" onmouseout = "clearData ();"> pinehirst No. 2 </td> </tr>
<Tr> <td id = "3" onmouseover = "getCourseData (this);" onmouseout = "clearData ();"> St. Andrews Links </td> </tr>
<Tr> <td id = "4" onmouseover = "getCourseData (this);" onmouseout = "clearData ();"> Baltusrol Golf Club </td> </tr>
</Tbody>
</Table>
<Div style = "position: absolute;" id = "popup">
<Table id = "courseData" bgcolor = "# FFFAFA" border = "0" ellspacing = "2" cellpadding = "2"/>
<Tbody id = "courseDataBody"> </tbody> </table>
</Div> </body>

</Html>
CourseData class

Using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;

/// <Summary>
/// Summary of CourseData
/// </Summary>
Public class CourseData
{
Public CourseData ()
{
//
// TODO: add the constructor logic here
//
}
Private int par;
Private int length;
Public CourseData (int par, int length) {this. par = par; this. length = length ;}
Public int getPar () {return this. par ;}
Public int getLength () {return this. length ;}
}

Handler. ashx

 

<% @ WebHandler Language = "C #" class = "Handler" %>

Using System;
Using System. Web;
Using System. Collections;

Public class Handler: IHttpHandler {
Private Hashtable courses = new Hashtable ();
Public void ProcessRequest (HttpContext context ){

CourseData augusta = new CourseData (72,729 0 );
CourseData pinehirst = new CourseData (70,721 4 );
CourseData standrews = new CourseData (72,656 6 );
CourseData maid = new CourseData (70,739 2 );
Courses. Add (1, augusta );
Courses. Add (2, pinehirst );
Courses. Add (3, standrews );
Courses. Add (4, baltusrol );

Int key = int. Parse (context. Request. QueryString ["key"]);
CourseData data = (CourseData) courses [key];
Context. Response. ContentType = "text/xml ";
Context. Response. AddHeader ("Cache-Control", "no-cache ");
Context. Response. Write ("<response> ");
Context. Response. Write ("<par>" + data. getPar () + "</par> ");
Context. Response. Write ("<length>" + data. getLength () + "</length> ");
Context. Response. Write ("</response> ");
Context. Response. End ();
}
 
Public bool IsReusable {
Get {
Return false;
}
}

}

Related Article

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.