首先在ASP.Net創建兩個WebForm頁,分別命名為Default1,Default2。下面給出代碼清單:
//Default1.ASPx
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default1.ASPx.cs" Inherits="Default1" %>
<!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 runat="server">
<title>無標題頁</title>
<script type="text/Javascript">
var XMLHttp;
function createXMLHttpRequest()
{
if (window.XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
XmlHttp = new ActiveXObject("MsXml2.XMLHTTP");
}
catch (e1)
{
try
{
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{}
}
}
return XMLHttp;
}
function DOStart() {
createXMLHttpRequest();
var url = "Default2.ASPx?task=reset";
XMLHttp.open("POST", url, true);
XMLHttp.onreadystatechange = startCallback;
XMLHttp.send(null);
}
function startCallback() {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
setTimeout("pollServer()", 1000);
refreshTime();
}
else { alert("HTTP error: "+XMLHttp.status);
}
}
}
function pollServer() {
createXMLHttpRequest();
var url = "Default2.ASPx?task=foo";
XMLHttp.open("POST", url, true);
XMLHttp.onreadystatechange = pollCallback;
XMLHttp.send(null);
}
function refreshTime(){
var time_span = document.getElementById("time");
var time_val = time_span.innerHtml;
var int_val = parseInt(time_val);
var new_int_val = int_val - 1;
if (new_int_val > -1) {
setTimeout("refreshTime()", 1000);
time_span.innerHtml = new_int_val;
} else {
time_span.innerHtml = 1;
}
}
function pollCallback() {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
var message = XmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
if (message != "done") {
var new_row = createRow(message);
var table = document.getElementById("dynamicUpdateArea");
var table_body = table.getElementsByTagName("tbody").item(0);
var first_row = table_body.getElementsByTagName("tr").item(1);
table_body.insertBefore(new_row, first_row);
setTimeout("pollServer()", 1000);
refreshTime();
}
}
&n
bsp; else {
alert("HTTP error: "+XMLHttp.status);
}
}
}
function createRow(message) {
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell_data = document.createTextNode(message);
cell.appendChild(cell_data);
row.appendChild(cell);
return row;
}
</script>
</head>
<body>
<form id="form1" runat="server" >
<h1>AJax Dynamic Update Example</h1>
This page will automatically update itself:
<input type="button" value="Launch" id="go" onclick="DOStart();"/>
<p/>
Page will refresh in <span id="time">1</span> seconds.
<p/>
<table id="dynamicUpdateArea" align="left">
<tbody>
<tr id="row0"><td></td></tr>
</tbody>
</table>
</form>
</body>
</Html>
//Default2.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;
public partial class Default2: System.Web.UI.Page
{
private static int counter = 1;
protected void Page_Load(object sender, EventArgs e)
{
String res = "";
String task = this.Request.Params["task"];
String message = "";
if (!string.IsNullOrEmpty(task))
{
if (task.Equals("reset"))
{
counter = 1;
}
else
{
switch (counter)
{
case 1: message = "Steve walks on stage"; break;
case 2: message = "iPods rock"; break;
case 3: message = "Steve says Macs rule"; break;
case 4: message = "Change is coming"; break;
case 5: message = "Yes, OS X runs on Intel - has for years"; break;
case 6: message = "Macs will soon have Intel chips"; break;
case 7: message = "done"; break;
}
counter++;
}
res = "<message>" + message + "</message>";
Response.ContentType = "text/XML";
Response.AppendHeader("Cache-Control", "no-cache");
Response.Write("<response>");
Response.Write(res);
Response.Write("</response>");
Response.End();
}
}
}