东方游人

E-freer's blog
posts - 24, comments - 6, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

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

Posted on 2009-04-10 16:42 东方游人 阅读(1561) 评论(0) 编辑 收藏

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.onreadystatechange = function()
        {
            //设置onReadyStateChange作为回调函数
            //将异步通讯对象作为this的上下文
            onReadyStateChange.apply(xhr);
        }
        xhr.send(null);
    }
    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";

        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啦!!!