asp.net用xmlhttp实现无刷新定时读取后台数据

来源:互联网 发布:360压缩软件 mac 编辑:程序博客网 时间:2024/05/15 01:43
 
http://www.cnblogs.com/efreer/archive/2009/04/10/1433213.html
整理

update.html页面所有代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>Untitled Document</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
    function getXMLHttpRequest()
    {
        if (window.XMLHttpRequest)
        {
            //适用于firefox浏览器创建异步通讯对象
            return new window.XMLHttpRequest();
        }
        else
        {
            //适用于IE来创建异步通讯对象,两个是不同的版本
            var progIDs = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ];
            for (var i = 0; i < progIDs.length; i++)
            {
                try
                {
                    var xmlHttp = new ActiveXObject(progIDs[i]);
                    return xmlHttp;
                }
                catch (ex) { }
            }
            return null;
        }
    }
    function sendRequest()
    {
        var xhr = getXMLHttpRequest();
        xhr.open("POST", "ajax.ashx");

       //切记下面一行

        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        //设置准备状态改变的回调函数
        xhr.onreadystatechange = function()
        {
            //设置onReadyStateChange作为回调函数
            //将异步通讯对象作为this的上下文
            onReadyStateChange.apply(xhr);
        }

        //发送的数据注意转义escape
         xhr.send("region=" + escape(points));

    }
    function onReadyStateChange()
    {
                                          //异步通讯对象的readyState的5种状态
                                         //0 - (未初始化)还没有调用send()方法
                                         //1 - (载入)已调用send()方法,正在发送请求
                                         //2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
                                      //3 - (交互)正在解析响应内容
                                      //4 - (完成)响应内容解析完成,可以在客户端调用了
        if (this.readyState == 4)
        {
            //Http响应状态,值很多,但是我们只需要知道200为正常返回
            if (this.status == 200)
            {
              document.getElementById("container").innerHTML= this.responseText;
           // alert(this.responseText);
            }
            else
            {
                throw new Error();
            }
        }
    }
function Button1_onclick() {
window.setInterval("sendRequest()", 1000);//定时刷新}

</script>
<style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
-->
</style></head>
<body onLoad="Button1_onclick();">
<div id="container"><!--容器--></div>
</body>
</html>

ajax.ashx代码:

<%@ WebHandler Language="C#" Class="ajax" %>

using System;
using System.Web;
using TaShop.BusinessLogicLayer;
using TaShop.Entity;

public class ajax : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

       //从post的数据中取出

       //MessageBox.Show(context.Request.Params["region"]);

        UserinfoEntity ue = new UserinfoEntity();
        try
        {
            ue = Userinfo_BLLSub.Get_UserinfoEntity(int.Parse(context.Request.Cookies["userinfo"]["userid"].ToString()));
            int num = 0;
            num = Msessage_BLLSub.isread(ue.userid, 0) + Msessage_BLLSub.isread(ue.userid, 1) + Msessage_BLLSub.isread(ue.userid, 2);
            if (num>0)
            {

                context.Response.Write("<strong><a href=\"user_msg.aspx\" target=\"mainFrame\" >未读消息(" + num + ")</a></strong>");
            }
            else
            {
            }
        }
        catch
        {

        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

这也是ajax的基本原来,不用任何插件,呵呵,用的时候可以直接frame update.html也可以直接复制update.html中的script代码,对控件写入就ok啦!!!

 

ashx

编辑本段ashx是什么文件?

  .ashx 文件用于写web handler的。.ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。
  .ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。

代码示例:

  当然你完全可以用.aspx 的文件后缀。使用.ashx 可以让你专注于编程而不用管相关的WEB技术。.ashx必须包含IsReusable. 如下例所示
  <% @ webhandler language="C#" class="AverageHandler" %>
  using System;
  using System.Web;
  public class AverageHandler : IHttpHandler
  {
  public bool IsReusable
  { get { return true; } }
  public void ProcessRequest(HttpContext ctx)
  {    
  ctx.Response.Write("hello");
  }
  }
  .ashx比.aspx的好处在于不用多一个html

 

 
原创粉丝点击