asp.net用xmlhttp实现无刷新定时读取后台数据
来源:互联网 发布:360压缩软件 mac 编辑:程序博客网 时间:2024/05/15 01:43
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是什么文件?
代码示例:
- asp.net用xmlhttp实现无刷新定时读取后台数据
- ASP.NET中运用xmlhttp实现无刷新数据传递(C# and JavaScript)
- xmlhttp定时无刷新更新数据库数据
- 利用XMLHTTP实现ASP.NET无刷新提交
- Asp.Net XmlHttp实现无刷新三联动下拉框
- asp.net实现ListBox、DropDownList无刷新三级联动(xmlhttp)
- 利用XMLHTTP实现ASP.NET无刷新提交
- 利用XMLHTTP实现ASP.NET无刷新提交
- 利用XMLHTTP实现ASP.NET无刷新提交
- ASP.NET中用XMLHTTP实现DropDownList无刷新联动
- 用XMLHTTP无刷新读取,增加,修改数据
- 用XMLHTTP无刷新读取,增加,修改数据
- Ajax实现定时刷新,获取后台数据(实现技术ASP)
- Ajax实现定时刷新,获取后台数据(实现技术ASP)
- Ajax实现定时刷新,获取后台数据(实现技术ASP)
- 用xmlhttp脚本实现无刷新返回数据
- XMlHTTP ajax实现数据无刷新调用
- Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
- DestroyWindow函数解析
- 程序的运行过程
- 查询和删除表中重复数据sql语句
- 图片处里blog address
- 微博实名制条件是否已经充分
- asp.net用xmlhttp实现无刷新定时读取后台数据
- 评委打分.cpp
- Android 加密解密
- Silverlight开发历程—C#代码添加动画
- (转)C语言学习札记(5)-- 字符和字符串
- 使用 Python 和 Oracle 数据库实现高并发性
- 完数.cpp
- 调试php没法使用smtp发送邮件的问题
- 开发者需知:优秀程序设计的18大原则