日期:2014-05-18  浏览次数:20412 次

关于用户验证的问题
CS文件
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {
  string name = this.Request.QueryString["name"];
  if (name != null)
  {
  OleDbConnection con = new OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/DB_SP.mdb"));
   
  string sqlstr = "select Count([Name]) from Member1 where [Name]=@Name";
  OleDbCommand cmd = new OleDbCommand(sqlstr, con);
  cmd.Parameters.Add("@Name",OleDbType.VarChar,50).Value=name;
  con.Open();
  int count = (int)cmd.ExecuteScalar();
  con.Close();
  if (count==0)
  {
  Label1.Text = "该用户名可以使用";
  }
  else
  {
  Label1.Text = "该用户名已经被占用,请使用其他用户名";
  }
  }
   
  }
}
aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 type="text/javascript">
  var xmlHttp = false;
  /*@cc_on @*/
  if (@_jscript_version >= 5)
  try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
  try {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
  xmlHttp = false;
  }
  if(!xmlhttp)
  {
  alert("出错拉!");
  }
  }

  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
  }
  function Validation()
  {
  var text=document.getElementById("TextBox1");
  xmlHttp.open("Post","Default.aspx?name="+text.value);
  xmlHttp.onreadystatechange=OnMessageBack;
  xmlHttp.send(null);
  }
  function OnMessageBack()
  {
  //判断请求状态及HTTP状态是否都能满足条件
  if (xmlHttp.readystate==4&&xmlHttp.status==200)
  {
  //将返回的文本打印到页面上
  var response=xmlHttp.responseText;
  document.write(response);
  }
  }

  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:Te