且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

会话超时时提醒用户

更新时间:2021-09-22 22:22:43

尝试AJAX

try AJAX

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ajaxtimer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hid_Ticker.Value = new TimeSpan(0, 0, 0).ToString();
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        hid_Ticker.Value = TimeSpan.Parse(hid_Ticker.Value).Add(new TimeSpan(0, 0, 1)).ToString();
        lit_Timer.Text = "Time spent on this page: " + hid_Ticker.Value.ToString();
        if (hid_Ticker.Value == "00:00:15")
        {
            Response.Redirect("http://"+fld_Name.Text);
        }
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        lit_Name.Text = "Thanks. You are redirected to: " + fld_Name.Text;
    }
}




检查此帖子
Hi,

Check this post


希望这会解决您的问题. .

创建一个warning.aspx页面,并在其中放置ur警告消息和确定"按钮..
并创建一个全局js文件,并将以下代码放入其中.
Hope this will sort ur problem...

cretae a warning.aspx page and put ur warning message and Ok button there..
and create a global js file and place the below code inside it.
   function Timeout()
   {
       //have hardcoded the session timeout below..u can get it from code behind
       window.setTimeout(DisplayWarning,90000-12000)//this will display warning after 13 min from page load
   }
   function DisplayWarning()
   {
      //display ur warning here
       var  newwindow =window.open( "warning.aspx", "WarningTitle",  "channelmode=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=330,height=220", true);
       newwindow.focus();
Timeout();
   }


在每个页面上导入此js ..并在onload上调用Timeout()函数,如..< body onload ="Timeout();"> ...在这里,您可以想到更好的解决方案,例如从一个实例调用timeout函数常见的地方..例如,有一些父类

在warning.aspx页面中,添加以下代码


Import this js on every page..and call Timeout() function on onload like ..<body onload="Timeout();">...here u can think of a better solution like calling the timeout function from one common place ..ex.having some parent class

In warning.aspx page add below code

function CloseWindow()
{
   var bclicked = "<%=bclicked%>";
    if(bclicked ==  "True" || bclicked ==  "true" )
    {
        window.close();
    }
}


在onload上调用此CloseWindow()函数,如<body onload="CloseWindow();">

并且在代码背后的代码中应该是..


Call this CloseWindow() function on onload like <body onload="CloseWindow();">

and In code behind the code shud be..

public bool bclicked = false;
protected void Page_Load(object sender, EventArgs e)
{
    bclicked = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
    bclicked = true;
}