大家好,我們繼續(xù)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()
?
描述:服務端經(jīng)常會用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 />
訪問次數(shù):<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對象,輸出計數(shù)器
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"]屬性不為空,則輸出當前用戶名,并開始網(wǎng)頁計數(shù)器,每打開該頁面一次(如刷新)就會使計數(shù)器加1,這也正是我第一篇博文里提到Application對象的用武之地。如果你長時間不動Application頁面,你發(fā)現(xiàn)該頁面不能刷新了,因為Session的保質期(我設了1分鐘)到了,Session信息清空,服務器不認識你了,又把你打回Login頁面了,哈哈,就是這么簡單~
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

