其实,是很简单的一个模块,只是以前自己写的,是在处理代码中验证用户输入,其实,在页面中加入验证控件来验证,会方便很多。
页面代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!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>
<table>
<tr>
<td colspan="3" align="center">用户登录</td>
</tr>
<tr>
<td>用户名:</td>
<td style=" 2px">
<asp:TextBox ID="TxtUser" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="请输入用户名" ControlToValidate="TxtUser"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>密码:</td>
<td style=" 2px">
<asp:TextBox ID="TxtPwd" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="请输入密码" ControlToValidate="TxtPwd"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="BtnLogin" runat="server" Text="登录" OnClick="BtnLogin_Click" /></td>
<td></td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="LbMeassage" runat="server" Text="用户名或密码不正确,请重新输入!"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
页面处理代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
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 System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
private static string connString = @"Server=localhost;Integrated Security=True; Database=Test";
protected void Page_Load(object sender, EventArgs e)
{
LbMeassage.Visible = false;
}
protected void BtnLogin_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (IsUserIdentify())
{
//定向到浏览页面
Response.Redirect("Default.aspx");
}
else
{
LbMeassage.Visible = true;
}
}
}
/// <summary>
/// 用户登录
/// </summary>
/// <returns></returns>
private bool IsUserIdentify()
{
string UserName=TxtUser.Text.Trim();
string Password=TxtPwd.Text.Trim();
SqlConnection con = new SqlConnection(connString);
string strSql="select * from tb_user where UserName='"+UserName+"' and Password='"+Password+"'";
SqlCommand cmd = new SqlCommand(strSql, con);
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//保存登录名到Session
Session["username"] = dr["UserName"].ToString();
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
cmd.Dispose();
con.Close();
}
}
}
页面定向:
Response.Redirect("Default.aspx");
Session的使用:
Session["username"] = dr["UserName"].ToString();