一个无刷新效果定时自动更新页面的例子(ASP.NET2.0-应用xmlhttp)

这是我从《Ajax基础教程》(作者:(美)阿斯利森 舒塔 人民邮电出版社 出版时间:2006年02月)第四章第四节中改写过来的例子,在ASP.NET2.0环境中应用XMLHTTP实现无刷新效果的定时自动更新页面。原文展示的是在Java环境中的应用。

首先在ASP.NET创建两个WebForm页,分别命名为Default1,Default2。下面给出代码清单:

//Default1.aspx

<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %>





  无标题页
 
 


 


   

Ajax Dynamic Update Example



    This page will automatically update itself:

     

   



    Page will refresh in 1 seconds.

   



   



   

     

   

 

 




//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 + "";


        Response.ContentType = "text/xml";

        Response.AppendHeader("Cache-Control", "no-cache");

        Response.Write("");

        Response.Write(res);

        Response.Write("
");

        Response.End();
    }

  }

}



qltouming(缘木渔人)
2007-02-06

你可能感兴趣的:(一个无刷新效果定时自动更新页面的例子(ASP.NET2.0-应用xmlhttp))