login.aspx
<%@ Page Language="c#" Inherits="MyBBS.Web.Login" CodeFile="Login.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
<link href="Styles/Style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<form id="Form1" method="post" runat="server">
<table id="Table1" style="z-index: 101; left: 15px; width: 328px; position: absolute;
top: 17px; height: 158px" cellspacing="0" cellpadding="0" width="328" border="0">
<tr>
<td style="width: 45px">
</td>
<td style="width: 164px" align="center">
<img alt="" src="Images/logo.JPG" width="130"/></td>
</tr>
<tr>
<td style="width: 45px">
<asp:Label ID="Label1" runat="server">登录名*</asp:Label></td>
<td style="width: 164px">
<asp:TextBox ID="TextBoxLoginName" runat="server" Width="160px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 45px">
<asp:Label ID="Label2" runat="server">密码*</asp:Label></td>
<td style="width: 164px">
<asp:TextBox ID="TextBoxPassword" runat="server" Width="160px" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:Button ID="ButtonLogin" runat="server" Width="56px" Text="登录" OnClick="ButtonLogin_Click">
</asp:Button>
<asp:Button ID="ButtonGuest" runat="server" Width="50px" Text="游客" OnClick="ButtonGuest_Click">
</asp:Button> </td>
</tr>
</table>
</form>
</body>
</html>
login.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MyBBS.BusinessLogicLayer;
namespace MyBBS.Web
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
/// <summary>
/// 用户单击“登录”按钮事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonLogin_Click(object sender, System.EventArgs e)
{
//获取用户在页面上的输入
string userLoginName = TextBoxLoginName.Text; //用户登录名
string password = TextBoxPassword.Text; //密码
Session.Add("login_name", userLoginName); //使用Session来保存用户登录名信息
User user = new User(); //实例化User类
user.LoadData(userLoginName); //利用User类的LoadData方法,获取用户信息
if (user.Exist) //如果用户存在
{
if (user.Password == password) //如果密码,转入留言列表页面
{
//Response.Redirect("Homepage.aspx");
Response.Write("<script language='javascript'>window.open('" + "Homepage.aspx" + "')</script>");
}
else //如果密码错误,给出提示,光标停留在密码框中
{
Response.Write("<Script Language=JavaScript>alert(\"密码错误,请重新输入密码!\")</Script>");
}
}
else //如果用户不存在
{
Response.Write("<Script Language=JavaScript>alert(\"对不起,用户不存在!\")</Script>");
}
}
/// <summary>
/// 用户单击“游客”按钮事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonGuest_Click(object sender, System.EventArgs e)
{
Session.Add("login_name", "guest"); //使用Session来保存用户登录名信息
Response.Write("<script language='javascript'>window.open('" + "Homepage.aspx" + "')</script>");
}
}
}