DAL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Da
namespace DAL
{
public static class UserService
{
//检查用户名是否存在
public static bool CheckUserName(string username)
{
string sql = "select count(*) from My_UserMessage where uname = '" + username + "'";
try
{
int count = DBHelper.GetScalar(sql);
if (count > 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using DAL;
namespace BLL
{
public static class UserManager
{
//检查用户是否存在
public static bool CheckUserName(string username)
{
return UserService.CheckUserName(username);
}
}
}
MODEL层
using System;
using System.Collections.Generic;
using System.Text;
namespace MODEL
{
public class UserMessage
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string uname;
public string Uname
{
get { return uname; }
set { uname = value; }
}
private string password;
public string Password
{
get { return password; }
set { password = value; }
}
}
}
Web层
CheckUserName.aspx代码
(*注:文本框的AutoPostBack="true"的属性)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckUserName.aspx.cs" Inherits="CheckUserName" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtCheck" runat="server" On
<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
CheckUserName.aspx.cs代码
using System;
using System.Da
using System.Configuration;
using System.Collections;
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 BLL;
using MODEL;
public partial class CheckUserName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//文本框改变事件
protected void txtCheck_TextChanged(object sender, EventArgs e)
{
bool result = UserManager.CheckUserName(txtCheck.Text.Trim());
if (result == true)
{
lblMessage.Text = "恭喜您,此用户名可以使用!";
Button1.Enabled = true;
}
if (result == false)
{
lblMessage.Text = "该用户名已存在,请更换用户名!";
Button1.Enabled = false;
}
}
}