ASP.NET 图书管理注册页面代码实现
时间: 2023-09-20 16:04:32 浏览: 120
ASP.NET实现图书管理系统的步骤详解
以下是ASP.NET图书管理系统注册页面的代码实现:
ASP.NET代码:
```
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="BookManagementSystem.Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注册页面</title>
<link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="main">
<h1>注册页面</h1>
<div class="registration-form">
<div class="form-group">
<label for="txtUserName">用户名:</label>
<asp:TextBox ID="txtUserName" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUserName" runat="server" ControlToValidate="txtUserName" ErrorMessage="* 请输入用户名"></asp:RequiredFieldValidator>
</div>
<div class="form-group">
<label for="txtPassword">密码:</label>
<asp:TextBox ID="txtPassword" runat="server" CssClass="form-control" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword" ErrorMessage="* 请输入密码"></asp:RequiredFieldValidator>
</div>
<div class="form-group">
<label for="txtConfirmPassword">确认密码:</label>
<asp:TextBox ID="txtConfirmPassword" runat="server" CssClass="form-control" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvConfirmPassword" runat="server" ControlToValidate="txtConfirmPassword" ErrorMessage="* 请输入确认密码"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvConfirmPassword" runat="server" ControlToCompare="txtPassword" ControlToValidate="txtConfirmPassword" ErrorMessage="* 两次输入的密码不一致"></asp:CompareValidator>
</div>
<div class="form-group">
<label for="txtEmail">邮箱:</label>
<asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="* 请输入邮箱"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="* 请输入正确的邮箱格式" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</div>
<div class="form-group">
<asp:Button ID="btnRegister" runat="server" CssClass="btn btn-primary" Text="注册" OnClick="btnRegister_Click" />
</div>
</div>
</div>
</form>
</body>
</html>
```
C#代码:
```csharp
using System;
using System.Data.SqlClient;
namespace BookManagementSystem
{
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string password = txtPassword.Text.Trim();
string email = txtEmail.Text.Trim();
// 检查是否有相同的用户名
string selectSql = "SELECT COUNT(*) FROM Users WHERE UserName=@UserName";
SqlCommand selectCmd = new SqlCommand(selectSql, DbConnection.GetConnection());
selectCmd.Parameters.AddWithValue("@UserName", userName);
int count = Convert.ToInt32(selectCmd.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('该用户名已存在,请重新输入!')</script>");
return;
}
// 插入用户信息到数据库
string insertSql = "INSERT INTO Users(UserName,Password,Email) VALUES(@UserName,@Password,@Email)";
SqlCommand insertCmd = new SqlCommand(insertSql, DbConnection.GetConnection());
insertCmd.Parameters.AddWithValue("@UserName", userName);
insertCmd.Parameters.AddWithValue("@Password", password);
insertCmd.Parameters.AddWithValue("@Email", email);
insertCmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!')</script>");
}
}
}
```
这段代码实现了一个简单的图书管理系统的注册页面,包括输入用户名、密码、确认密码和邮箱,并且检查是否有相同的用户名,最后将用户信息插入到数据库中。
阅读全文