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是什么文件?
代码示例: