[研究][C#][ASP.NET][WebForm] Master Page 的 Session Time Out 自動登出前倒數計時
2020-11-17
續這篇
[研究][C#][ASP.NET][WebForm] Session Time Out 自動登出前倒數計時
http://shaurong.blogspot.com/2020/11/caspnetwebform-session-time-out.html
測試工具 Visual Studio 2019 v16.8.1 版
Site1.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <%--https://docs.microsoft.com/zh-tw/dotnet/api/system.web.ui.timer?view=netframework-4.8--%> <%--https://docs.microsoft.com/zh-tw/dotnet/api/system.web.sessionstate.httpsessionstate.timeout?view=netframework-4.8--%> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <%--Interval="1000" 是每1秒觸發一次--%> <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"></asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Button ID="Button_Timer" runat="server" BackColor="LightGreen" Height="40px" Width="100px" Style="border-radius: 8px" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="tick" /> </Triggers> </asp:UpdatePanel> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html> |
Site1.Master.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Site1 : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { // -------------------------------------------------------------------------------- // 顯示 Session Time Out 時間 (Begin) int timeout = System.Web.HttpContext.Current.Session.Timeout; // 設定 Session Time Out 時間 20 分鐘 //if (!ScriptManager1.IsInAsyncPostBack) // Fail, 當 ScriptManager1 在 MasterPage 時候不能這樣存取 //if (!Master.ScriptManager1.IsInAsyncPostBack) // Fail //if (!this.Master.FindControl("ScriptManager1").IsInAsyncPostBack) // Fail ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); if (!scriptManager.IsInAsyncPostBack) { // 把目前的時間,加上 Session Time Out 設定值, //Session["timeout"] = DateTime.Now.AddMinutes(20).ToString(); Session["timeout"] = DateTime.Now.AddMinutes(timeout).ToString(); } // 顯示 Session Time Out 時間 (End) // -------------------------------------------------------------------------------- } protected void Timer1_Tick(object sender, EventArgs e) { if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString()))) { Button_Timer.Text = ((Int32)DateTime.Parse(Session["timeout"]. ToString()).Subtract(DateTime.Now).Minutes).ToString() + ":" + ((Int32)DateTime.Parse(Session["timeout"]. ToString()).Subtract(DateTime.Now).Seconds).ToString(); } } } } |
重點是使用 Master Page 時候, Site1.Master 中,
if (!ScriptManager1.IsInAsyncPostBack)
要改成
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (!scriptManager.IsInAsyncPostBack)
才行。
Default.aspx (不用特別去修改)
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> </asp:Content> |
Default.aspx.cs (不用特別去修改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } } |
Web.Config (預設 20 分鐘,可以不用特別去設定)
<configuration> <system.web> <sessionState timeout="20"></sessionState> </system.web> </configuration> |
(完)
相關
[研究] AjaxControlToolkit ToolkitScriptManager 元素不是已知元素
http://shaurong.blogspot.com/2020/11/ajaxcontroltoolkit-toolkitscriptmanager.html
[研究][JavaScript][C#][ASP.NET][WebForm] 主版頁面 Master Page 用 idle-timer.js 做 Session Time Out 前 N 秒自動彈出倒數計時對話盒視窗 (目前推薦)
https://shaurong.blogspot.com/2020/11/javascriptcaspnetwebform-master-page.html
[研究][JavaScript] 用 idle-timer.js 做 Session Time Out 前 N 秒自動彈出倒數計時對話盒視窗
http://shaurong.blogspot.com/2020/11/javascript-idle-timerjs-session-time.html
[研究][JavaScript][C#][ASP.NET][WenForm] 主版頁面 Master Page 用 timeout-dialog.js 做 Session Time Out 前 60 秒自動彈出倒數計時對話盒視窗
http://shaurong.blogspot.com/2020/11/javascriptcaspnetwenform-master-page.html
[研究][JavaScript] 用 timeout-dialog.js 做 Session Time Out 前 60 秒自動彈出倒數計時對話盒視窗
http://shaurong.blogspot.com/2020/11/javascript-timeout-dialogjs-session.html
[研究][C#][ASP.NET][WebForm] Sessionn Time Out 自動登出前倒數計時
http://shaurong.blogspot.com/2020/11/caspnetwebform-sessionn-time-out.html
[研究][C#][ASP.NET][WebForm] Master Page 的 Sessionn Time Out 自動登出前倒數計時 (目前推薦)
http://shaurong.blogspot.com/2020/11/caspnetwebform-master-page-sessionn.html
[研究][C#][ASP.NET][WebForm][JavaScript]顯示 Web.Config 設定 Session Out Time 時間
http://shaurong.blogspot.com/2020/11/caspnetwebformjavascript-webconfig.html
沒有留言:
張貼留言