ajax实现注册

这个小程序只有两个页面 一个是webfrom1.aspx 用做注册页面,一个是check.aspx 用来连接数据库检测是否存在相同的数据

全部代码如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ajax.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="vs_showGrid" content="False">
  <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">
  <script language="javascript" type="text/javascript">
   var xmlHttp = false;
   //不同版本的ie用不同的方法创建xmlHttp对象
   try {
   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
   try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e2) {
    xmlHttp = false;
   }
   }
   //非ie浏览器创建xmlHttp对象 该对象能够完成从数据包到Request对象的转换和发送任务
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
   xmlHttp = new XMLHttpRequest();
   }

   function callServer() {
   var u_name = document.getElementById("txtName").value;
   if ((u_name == null) || (u_name == "")) return;
   var url = "check.aspx?name=" + escape(u_name);
   //xmlHttp.open的参数有五个 1、提交的方式 2、要提交到的地址 3、是否异步 4、登录到服务器的id 5、登录到服务器的密码  该方法是对
   //Request对象进行初始化
   xmlHttp.open("GET", url, true);
   xmlHttp.onreadystatechange = updatePage;
   //传送xml数据
   xmlHttp.send(null); 
   }
            //服务器处理请求的进展状况
            //0、Request对象已经建立 但是xml上载过程尚未结束
            //1、xml装载完毕
            //2、xml装载完毕  正在处理中
            //3、部分xml文档已经解析
            //4、解析完毕 客户端可以接受返回的信息
            //
   function updatePage() {
   if (xmlHttp.readyState == 0) {
     test1.innerHTML="0";
   }
    if (xmlHttp.readyState == 1) {
     test1.innerHTML="1";
   }
    if (xmlHttp.readyState == 2) {
     test1.innerHTML="2";
   }
    if (xmlHttp.readyState == 3) {
     test1.innerHTML="3";
   }
   if (xmlHttp.readyState == 4) {
   //客户机接受相应 通过xmlHttp的属性来实现
   //responseText 将返回的信息作为文本
   //responseBody 将返回的信息做为html文档内容
   //responseXml  将返回的信息视为xml文档
   //responseStream 将返回的信息作为Stream对象
    var response = xmlHttp.responseText;
     test1.innerHTML=response;
   }
   }
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体"><INPUT style="Z-INDEX: 101; LEFT: 320px; POSITION: absolute; TOP: 120px" type="text" id="txtName" onchange="callServer();">
    <span id="test1" style="Z-INDEX: 102; LEFT: 512px; POSITION: absolute; TOP: 120px">是否能注册</span><br>
    <INPUT style="Z-INDEX: 103; LEFT: 320px; POSITION: absolute; TOP: 168px" type="text"></FONT>
   <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 352px; POSITION: absolute; TOP: 232px" runat="server"
    Text="Button"></asp:Button>
  </form>
 </body>
</HTML>



check.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace ajax
{
 /// <summary>
 /// check 的摘要说明。
 /// </summary>
 public class check : System.Web.UI.Page
 {
  protected static string connectionString = ConfigurationSettings.AppSettings["strConn"];
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Response.Clear();
   if(!this.IsPostBack)
   {
    string name=Request.QueryString["name"].ToString();
    SqlConnection con=new SqlConnection(connectionString);
    con.Open();
    SqlCommand cmd=new SqlCommand("select count(*) from sf_user where user_Name='"+name+"'",con);
    int count=Convert.ToInt32(cmd.ExecuteScalar());
    if(count>0)
    {
     Response.Write("这个用户名已存在");
    }
    else
    {
     Response.Write("你可以使用这个用户名");
    }
    con.Close();
   }
   Response.End();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

posted @ 2007-03-21 17:47  小飞龙  阅读(1017)  评论(1编辑  收藏  举报