An example of a page with no refresh effect timed to update automatically

Source: Internet
Author: User
Tags reset window
Timed | refresh | no Refresh | page

An example of a page with no refresh effect timed automatically update (asp.net2.0-application XMLHTTP)

First create two WebForm pages in asp.net, named Default1,default2 respectively. Here's a list of code:

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 ">
<title> Untitled Page </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 ();
}
}
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>
<body>
<form id= "Form1" runat= "Server" >
This page would automatically update itself:
<input type= "button" value= "Launch" id= "Go"/>
<p/>
Page'll 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>

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 = "The 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'll 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 ();
}
}
}



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.