大家好,我们继续ASP.NET之旅~今天讲的是Session对象以及Response对象,并复习Application对象的有关知识,做一个简单的登录跳转页面。首先还是了解下Session、Response两个对象。
*** Session对象 ***
语法:Session["属性名"]
Session.Timeout,Session.SessionID
SessionAbandon()
描述:Session对象用于存储特定的用户所需信息,当页面跳转时Session对象中的信息是不会清除的。不过Session对象也是有“保质期”的,记录在“Timeout”属性中,默认20分钟。当然如果你想早点结束Session,可以使用"Abandon()"方法显示结束一个会话(Session的中文意义)
*** Session对象 ***
*** Response对象 ***
语法:Response.Write(),Response.Redirect(),Response.End()
描述:服务端经常会用Response对象的一些方法向客户端(浏览器)输出信息。
*** Response对象 ***
用法如下——
Login.aspx
<%@ 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>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
用户:<asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnLogin" runat="server" onclick="btnLogin_Click" Text="登录" />
</div>
</form>
</body>
</html>
Login.aspx.cs
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Session["User"] = txtUser.Text.Trim ();
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//预设用户名为a
if (txtUser.Text == "a")
{
//Session对象记录下登录用户名
Session["User"] = txtUser.Text.Trim();
//登录成功,跳转至目的页
Response.Redirect("Application.aspx");
}
else
{
//登录失败,跳转回本页
Response.Redirect("Login.aspx");
}
}
}
Application.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Application.aspx.cs" Inherits="Application" %>
<!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>Application</title>
</head>
<body>
<form id="form1" runat="server">
<div>
登陆成功
<br />
访问次数:<asp:Label ID="lblCount" runat="server" Text="Label"></asp:Label>
次</div>
</form>
</body>
</html>
Application.aspx.cs
public partial class Application : System.Web.UI.Page
{
public const string COUNT="Count";
protected void Page_Load(object sender, EventArgs e)
{
//设置Session失效时间为1分钟
Session.Timeout = 1;
//利用Session对象,判断用户是否登录
if (Session["User"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
Response.Write("当前登录名为:"+Session ["User"].ToString ());
}
//利用Application对象,输出计数器
if (Application[COUNT] == null)
{
Application[COUNT] = 1;
lblCount .Text =Application [COUNT ].ToString () ;
}
else
{
//Application对象为Object类型,必须先转换为Int类型
Application[COUNT] = Convert.ToInt32(Application[COUNT]) + 1;
lblCount .Text =Application [COUNT ].ToString () ;
}
}
}
好,我来说下运行机制。首先进入的是Longin页面,因为比较简单,我只认同用户名为"a",点击登录按钮可以跳转,否则停留在本页。当正确输入用户名跳转至Application页面时,代码首先会验证Session["User"]属性是否为空,若为空则把你打回Longin页面(这是为了防止非法用户不登录而直接访问Application页面),若["User"]属性不为空,则输出当前用户名,并开始网页计数器,每打开该页面一次(如刷新)就会使计数器加1,这也正是我第一篇博文里提到Application对象的用武之地。如果你长时间不动Application页面,你发现该页面不能刷新了,因为Session的保质期(我设了1分钟)到了,Session信息清空,服务器不认识你了,又把你打回Login页面了,哈哈,就是这么简单~

