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;
}
}
}
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;
}
}
}
相关文章推荐
- 哪位高手帮我看看,为什么不能实现跳转到下一页或者上一页
- 为什么DoDragDrop在线程中不能正常?请高手看看。
- 关于Ajax无刷新分页技术的一些研究 c#
- asp.net mvc Ajax.BeginForm不能异步刷新,或转到新页面,或页面还是刷新了,的原因(或解决办法)
- AJAX 怎样在一个UpDatePanel中刷新另一个updatePanel (asp.net C#)
- 快照库MV不能成功刷新问题的解决
- 为什么安装了证书,却不能用,有时候钥匙串里也没有安装成功?
- C#中为什么不能再方法里定义静态变量(Static)
- c# 为什么结构中不能有实例字段初始设定值项
- 急需....C#中不能访问sql数据库,高手请进......
- 阿里云ApsaraCache的正式开源,为什么不能仅仅满足于商业上的成功?
- IE8用ajax访问不能每次都刷新的问题
- ajax请求成功后执行回调方法后页面会自动刷新问题
- 星巴克成功之谜:为什么不能因为利益牺牲原则?
- C# .NET AJAX无刷新技术
- ie8用ajax访问不能每次都刷新的问题
- 一个很简单的jquery+xml+ajax的无刷新树结构(无css,后台是c#)
- Ajax删除成功刷新页面但是数据刷新不了需要手动刷新---详解
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到
- 为什么自定义函数效验器和页面加载成功事件不能放在一起