您的位置:首页 > 编程语言 > C#

C# Ajax为什么我的刷新不能成功,高手看看

2009-08-30 22:39 316 查看
这是源码:帮我看看

Ajax.aspx 页面

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>

<script language="javascript" type="text/javascript">

function Execute()
{
Http_Request=false; //声明XMLHttpRequest

//初始化XMLHttpRequest
if(window.XMLHttpRequest)
{
//非IE浏览器
Http_Request=new XMLHttpRequest();
}else if(window.ActiveXObject){

try
{
/// alert("IE新版本");
Http_Request=new ActiveXObject("msxml2.XMLHTTP"); //IE新版本

}catch(e){
try
{
// alert("IE旧版本");
Http_Request=new ActiveXObject("microsoft.XMLHTTP"); //IE旧版本

}catch(e){ }
}
}else{
alert("不能创建XMLHttpReques对象,无法应用Ajax");
return false;
}

// 指定回调函数
Http_Request.onReadyStateChange=login;
alert("“指定回调函数”也执行了的");
//创建Http请求
Http_Request.Open("get","Handler.ashx?txtLogin="+document.getElementById("txtLogin").value,true);

//发送Http请求
Http_Request.send(null);

}

function login()
{

if(Http_Request.readyState==4) //是否发送成功
{
if(Http_Request.status==200) //是否交易成功
{
//正式开始处理数据
document.getElementById("lblMessage").innerText=Http_Request.responseText;
}
}
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 140px; position: relative;
top: 108px" Text="账号:"></asp:Label>
<asp:TextBox ID="txtLogin" runat="server" onBlur="Execute()" Style="z-index: 101; left: 192px; position: relative;
top: 104px" AutoCompleteType="Disabled"></asp:TextBox>
<asp:TextBox ID="txtPass" runat="server" Style="z-index: 102; left: 36px; position: relative;
top: 176px" AutoCompleteType="Disabled"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Style="z-index: 103; left: -226px; position: relative;
top: 176px" Text="密码:"></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Style="z-index: 104; left: 296px; position: absolute;
top: 280px" Text="提交" />
<asp:Label ID="lblMessage" runat="server" Style="z-index: 106; left: 8px; position: relative;
top: 104px"></asp:Label>
</div>
</form>
</body>
</html>

Handler.ashx 页面

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

using System;
using System.Web;

public class Handler : IHttpHandler {

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

string login = context.Request.QueryString["txtLogin"].ToString();

int num = 1;
if (num > 0)
{
context.Response.Write("该用户已经存在!");
}
else
{
context.Response.Write("可以是用[" + login + "]");

}

context.Response.Write("Hello World");
}

public bool IsReusable {
get {
return false;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐