1、Default.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">
<script src="js/JScript.js" type="text/javascript"></script>
<title></title>
<style type="text/css">
.Error{ border:1px solid #A2553B;background-color:#FEF1E1;width:200px;float:left;height:18px;line-height:18px;margin-left:24px;color:#4F4F4F;text-indent:12px;}
.Right{ border:1px solid #419041;background-color:#EDFCE7;width:200px;float:left;height:18px;line-height:18px;margin-left:24px;color:#4F4F4F;text-indent:12px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="lbl" runat="server" Text="填写用户名" ForeColor="Red">
</asp:Label>
</td>
<td>
<asp:TextBox ID="txt1" οnblur="callserver()" runat="server"></asp:TextBox>
</td>
<td>
<div id="div1">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
2、check_id.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="check_id.aspx.cs" Inherits="check_id" %>
3、check_id.aspx.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class check_id : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.QueryString["strtxt"].ToString();
bool YHM = CheckUser(name);
// if (name == "123"||name=="456")
if(YHM==false)
{
//123不能注册。。。
Response.Write("error");
}
else
{
Response.Write("regok");
}
}
public bool CheckUser(string name)
{
//SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings[0]);
//SqlCommand Cmd = new SqlCommand("select id from USER where NAME='" + name + "'", Conn);
//Conn.Open();
//SqlDataReader DR = Cmd.ExecuteReader();
//return DR.Read();
//Conn.Close();
return false;
}
}
4、js
var http_request = false;
function createRequest() {
try//IE
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
http_request = false;
}
}
if (!http_request && typeof XMLHttpRequest != 'undefined') //FireFox
{
http_request = new XMLHttpRequest();
}
}
function callserver() {
createRequest();
var strText = document.getElementById("txt1").value;
if (strText == "" || strText == null)
return;
var url = "check_id.aspx?strtxt=" + escape(strText);
http_request.onreadystatechange = updatepage;
http_request.open("GET", url, true);
http_request.send(null);
}
function updatepage() {
var obj = document.getElementById("div1");
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var response = http_request.responseText;
if (response.indexOf("regok") != -1) {
//验证通过
obj.className = "Right";
obj.innerHTML = '该会员名可以注册。';
}
else {
//验证未通过
obj.className = "Error";
obj.innerHTML = '该会员名已经被注册。';
}
}
}
else { //异步传输失败
//obj.innerHTML = '该会员未能检测,可以尝试注册。';
obj.innerHTML = '该会员名可以注册。';
}
}