大众计算机学习网

专门提供各种电脑教程

在ASP.NET中使用AJAX无刷新技术的简单方法

Admin | 2007-12-28 22:20:53 | 被阅次数 | 18903

  • 脚本代码

     var cuObject;

    //校验按钮单击事件
     function userCheck(par)
     {
      cuObject=par;
      var f = document.Form1;
      var valuenr;
      strUserName = f.username.value;
      if(strUserName=="")
      {
       alert("请输入用户名!");
       f.username.focus();
       return false;
      }
      else
      {    
          Send_Request('AjaxClass.aspx?username='+escape(strUserName));    
         }
     }

     var http_request = false;
     //初始化、指定处理函数、发送请求的函数;
     function Send_Request(url)
     {
      http_request = false;
      //开始初始化XMLHttpRequest  对象
      if(window.XMLHttpRequest)
      {
       //Mozilla 浏览器
       http_request = new XMLHttpRequest();
       if(http_request.overrideMimeType)
       {
       //设置MIME类别
        http_request.overrideMimeType("text/xml");    
       }   
      }
      else if(window.ActiveXObject)
      {
       //IE浏览器
       try{
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
        try{
         http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){}
       }
      }//初始OVER
      
      if(!http_request)
      {
      //异常,创建对象实例失败
       window.alert("不能创建XMLHttpRequest 对象实例.");
       return false;
      }
      
      //指定 XMLHttpRequest 响应处理函数
      http_request.onreadystatechange = processRequest;
      //确定发送请求的方式和URL以及是否同步执行下段代码
      http_request.open("Post",url,true);
      // 设置服务器的响应header,否则无法传大数据量数据。
      
      http_request.setrequestheader("cache-control","no-cache");
      http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
      http_request.send(null);
        
     }
     
     function processRequest()
     {
      //var strResult="";
      if(http_request.readyState==4)
      {
      //判断对象状态
       if(http_request.status==200)
       {
       //判断信息是否返回成功,开始处理信息
        //cuObject.innerHTML+=http_request.responseText;
        alert(http_request.responseText);
        //window.parent.location="Ulogin.aspx";
       }
       else
       {
       //页面不正常
        alert("您所请求的页面有异常");
       }
      }
      
     }

    html内容:

    <%@ Page language="c#"  Codebehind="AjaxClass.aspx.cs" AutoEventWireup="false" Inherits="Cshap.AjaxClass"  %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
     <HEAD>
      <title>AjaxClass</title>
      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" Content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
     </HEAD>
     <body MS_POSITIONING="GridLayout">
      <form id="Form1" method="post" runat="server">
       <INPUT style="Z-INDEX: 101; LEFT: 504px; POSITION: absolute; TOP: 16px" type="text" name="username2"><INPUT style="Z-INDEX: 102; LEFT: 688px; POSITION: absolute; TOP: 56px" type="submit" value="Submit"><INPUT style="Z-INDEX: 103; LEFT: 504px; WIDTH: 152px; POSITION: absolute; TOP: 56px; HEIGHT: 24px"
        type="button" value="唯一性检查" name="check" onclick="userCheck(nr1);">
       <TABLE id="Table1" style="Z-INDEX: 104; LEFT: 48px; WIDTH: 556px; POSITION: absolute; TOP: 280px; HEIGHT: 51px"
        cellSpacing="1" cellPadding="1" width="556" border="1">
        <TR>
         <TD id="nr1"></TD>
        <TR>
         <TD id="nr2"></TD>
        </TR>
       </TABLE>
       <TEXTAREA id="username" style="Z-INDEX: 105; LEFT: 56px; WIDTH: 424px; POSITION: absolute; TOP: 16px; HEIGHT: 168px"
        name="username" rows="10" cols="50">
       </TEXTAREA>
      </form>
      <script src="js/Http_Request.js" type="text/javascript"></script>

     </body>
    </HTML>