ASP.NET+Ajax初体验

      初步学习Ajax,希望能给还没入门的人带来一些帮助。
------------------------------------------------------------------------------------------------------------------------------------------
*.html文件
------------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
     <script language="java" type="text/javascript">
//Ajax 开始 by xu 2007-9-17*/
var http = getHTTPObject();
function handleHttpResponse()
{
    if(http.readyState == 4)
    {
        if(http.status == 200)
        {
          var xmlDocument = http.responseXML;
             if(http.responseText!="")
               {
                  document.getElementById("showStr").style.display = "";  
                  document.getElementById("userName").style.background= "#FF0000";
                  document.getElementById("showStr").innerText = http.responseText;

               }
               else
               {
                  document.getElementById("userName").style.background= "#FFFFFF";
                  document.getElementById("showStr").style.display = "none";
               }
        }
       else
        {
           alert("你所请求的页面发生异常,可能会影响你浏览该页的信息!");
            alert(http.status);
         }
   }
}

 function chkUser(){

  var url = "ajaxCheckUserName.aspx";

  var name = document.getElementById("userName").value;

  url += ("?userName="+name);
 //  window.open(url,"",width="300",height="300");
  http.open("GET",url,true);
  http.onreadystatechange = handleHttpResponse;

  http.send(null);

  return ;

 }

 function chkComNm()
  {

   var url = "/ajaxCheckUserName.aspx";
   var name = document.getElementById("comNm").value;

  url += ("&userName="+name+"&oprate=chkCom");

  http.open("GET",url,true);

  http.onreadystatechange = handleHttpResponse1;

  http.send(null);

  return ;

 }

 

 //该函数可以创建我们需要的XMLHttpRequest对象

 function getHTTPObject()
 {

  var xmlhttp = false;
  if(window.XMLHttpRequest)
  {
      xmlhttp = new XMLHttpRequest();

     if(xmlhttp.overrideMimeType)
     {

       xmlhttp.overrideMimeType('text/xml');

     }

  }

  else
  {
      try
      {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
       try
       {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

       }
       catch(e)
       {
            xmlhttp = false;
       }
    }
  }
  return xmlhttp;

}
 </script>

   
   
   
</head>
<body>

<table width="400" border="0" cellpadding="1" cellspacing="1" align="center" >

<tr>


<td><font color="red"> *</font>"</td>
<td>用户帐号:</td>

<td>

<input type="text" name="userName" maxlength="20" style="background:#FFFFFF" οnblur="chkUser()" value="" />

<div id="showStr" style="background-color:#FF9900;display:none"></div>
 <div id="comNmStr" style="background-color:#FF9900;display:none"></div>
</td>
</tr>
</table>
</body>
--------------------------------------------------------------------------------------------------------------------------------------
ajaxCheckUserName.aspx.cs文件
--------------------------------------------------------------------------------------------------------------------------------------
public partial class ajaxCheckUserName : System.Web.UI.Page
{
    string userName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        userName=Request.QueryString["userName"].ToString();
        validateUser(userName);
    }

    public void validateUser( string userName)
    {
        using(SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["users"].ToString()))
        {
              conn.Open();
              SqlCommand cmd=new SqlCommand("select * from userInfo where userName='" + userName +"'" );
              cmd.Connection = conn;
              if (cmd.ExecuteScalar() != null)
              {
                  Response.Write("用户已存在");
              }
              else
              {
                  Response.Write("恭喜你,可以注册");
              }

        }

    }
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值