aspNet,登陆功能模块,学习资料【完整版】
下面是小编为大家整理的aspNet,登陆功能模块,学习资料【完整版】,供大家参考。
前台代码<%@ 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 background="bg. gif">
<form id="form1" runat="server">
<div>
</div>
<table width=100% height=100%>
<tr>
<td style="width:
456px;
height:
200px; ">
</td>
<td style="width:
348px;
height:
200px; ">
</td>
<td style="height:
200px">
</td>
</tr>
<tr>
<td style="width:
456px;
height:
230px; ">
</td>
<td style="width:
348px;
height:
230px; ">
<table style="width:
355px;
border-right:
#cc00ff thin double;
border-top:
#cc00ff thin double;
border-left:
#cc00ff thin double;
border-bottom:
#cc00ff thin double; ">
<tr>
<td colspan="3" style="text-align:
center">
</td>
</tr>
<tr>
<td style="width:
70px">
用户名:
</td>
<td colspan="2">
<asp: TextBox ID="TextBox1" runat="server"></asp: TextBox><asp: RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="用户名不能为空"></asp: RequiredFieldValidator></td>
</tr>
<tr>
<td style="width:
70px">
密码:
</td>
<td colspan="2">
<asp: TextBox ID="TextBox2" runat="server" TextMode="Password" Width="150px"></asp: TextBox><asp: RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="密码不能为空"></asp: RequiredFieldValidator></td>
</tr>
<tr>
<td style="text-align:
center" colspan="3">
<asp: Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登陆" Width="68px" />
<asp: HyperLink ID="HyperLink1" runat="server">注册</asp: HyperLink></td>
</tr>
</table>
<asp: Image ID="Image1" runat="server" Height="147px" ImageUrl="~/logo. gif" Width="341px" /></td>
<td style="height:
230px">
</td>
</tr>
<tr>
<td style="width:
456px;
height:
248px; ">
</td>
<td style="width:
348px;
height:
248px; ">
</td>
<td style="height:
248px">
</td>
</tr>
</table>
</form> </body> </html>
Web.config<connectionStrings>
<add name="connection" connectionString="server=(local) ; database=news; uid=sa; pwd=" /> </connectionStrings>
登陆按钮后台代码
protected void Button1_Click(object sender,
EventArgs e)
{
//创建连接对象
SqlConnection conn = new SqlConnection(ConfigurationManager. ConnectionStrings["connection"]. ConnectionString) ;
//创建查询, 用户名是否存在数据对象
SqlCommand cmd = new SqlCommand("select * from login where 用户名=" " + TextBox1. Text + "" ",
conn) ;
try
{
//如果存在则打开数据库
conn. Open() ;
SqlDataReader sdr = cmd. ExecuteReader() ;
//如果用户名输入正确
if (sdr. Read() )
{
//判断密码是否正确
if (sdr["密码"]. ToString() ==TextBox2. Text)
{
//如果用户名和密码都正确就关闭数据库连接
conn. Close() ;
//将用户名存放到session中
Session["用户名"] = TextBox1. Text. Trim() ;
//并进入后台页面
Response. Redirect("admin_index. aspx") ;
}
else//否则
{
//弹出对话框, 提示密码错误
Response. Write("<script language=javascript>alert(" 密码错误!" ) </script>") ;
}
}
else//否则
{
//弹出对话框提示用户名错误
Response. Write("<script language=javascript>alert(" 用户名错误或不存在!" ) </script>") ;
}
}
catch(System. Exception ee) //异常处理
{
Response. Write("<script language=javascript>alert(" "+ee. Message. ToString() +"" ) </script>") ;
}
finally
{
conn. Close() ; //关闭数据库
}
}