图书借阅系统asp.net使用控件实现登录注册模块aspx代码以及后台c#代码并连接数据库2008
时间: 2024-03-24 22:41:41 浏览: 96
基于asp.net图书借阅管理系统设计与实现.docx
5星 · 资源好评率100%
以下是ASP.NET使用控件实现登录注册模块的aspx代码:
登录页面(Login.aspx):
```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Login</h1>
<hr />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
<br />
<br />
<asp:Label ID="lblUserName" runat="server" Text="User Name:"></asp:Label>
<br />
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
<br />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<br />
<br />
<a href="Register.aspx">Register</a>
</div>
</form>
</body>
</html>
```
注册页面(Register.aspx):
```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Register</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Register</h1>
<hr />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
<br />
<br />
<asp:Label ID="lblUserName" runat="server" Text="User Name:"></asp:Label>
<br />
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
<br />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password:"></asp:Label>
<br />
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" />
<br />
<br />
<a href="Login.aspx">Login</a>
</div>
</form>
</body>
</html>
```
以下是后台C#代码:
登录页面(Login.aspx.cs):
```
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
public partial class Login : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["UserName"] != null)
{
Response.Redirect("Default.aspx");
}
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
string query = "SELECT COUNT(1) FROM Users WHERE UserName=@UserName AND Password=@Password";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 1)
{
Session["UserName"] = txtUserName.Text.Trim();
Response.Redirect("Default.aspx");
}
else
{
lblMessage.Text = "Invalid User Name or Password";
}
}
}
}
}
```
注册页面(Register.aspx.cs):
```
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
public partial class Register : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
string query = "INSERT INTO Users (UserName, Password) VALUES (@UserName, @Password)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
cmd.ExecuteNonQuery();
lblMessage.Text = "Registration Successful";
txtUserName.Text = "";
txtPassword.Text = "";
txtConfirmPassword.Text = "";
}
}
}
}
```
假设数据库名为Library,用户表名为Users,包含两个字段:UserName和Password。以下是连接数据库的web.config配置文件:
```
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=SERVERNAME;Initial Catalog=Library;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
</configuration>
```
请注意替换连接字符串中的SERVERNAME为你的SQL Server实例名称。
阅读全文