Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have a problem that is when we are doing work on a page then session time alert the user,while we want when the page is in idle condition means there is no working on page,then session time should be warn the user.How to do this????

Thanks for help...
Posted

try AJAX

C#
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;
    }
}
 
Share this answer
 
Hi,

Check this post
 
Share this answer
 
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.
JavaScript
   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();
   }

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
JavaScript
function CloseWindow()
{
   var bclicked = "<%=bclicked%>";
    if(bclicked ==  "True" || bclicked ==  "true" )
    {
        window.close();
    }
}

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

and In code behind the code shud be..
C#
public bool bclicked = false;
protected void Page_Load(object sender, EventArgs e)
{
    bclicked = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
    bclicked = true;
}
 
Share this answer
 
v2
What you can do is have the session count down timer displaying in a label on the page, then crate a base page that you inherit your pages from.
Using the basepage you set the Page_PreInit event, then inject the timeout via javascript when the page is loaded. So when the page is postback, the session resets and the counter starts over. the session time you can set in you config file, so you can increase your session time if required at a later stage.

Here is a basic session counter that will update the label on the master page displaying the time left (lblSessionTime) and trigger the log off button on the master page (ctl00_lbtnLogOff) when the session expires.

C#
protected void Page_PreInit(object sender, EventArgs e)
        {         
            if (!this.Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "SessionTime"))
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "SessionTime", "var sessionTimeout = "
                    + Session.Timeout.ToString()
                    + "; function DisplaySessionTimeout() { document.getElementById('"
                    + this.Master.FindControl("lblSessionTime").ClientID
                    + @"').innerText = sessionTimeout; sessionTimeout = sessionTimeout - 1; if (sessionTimeout >= 0) window.setTimeout(""DisplaySessionTimeout()"", 60000); else {alert(""Your current Session is over.""); document.getElementById(""ctl00_lbtnLogOff"").click();}}", 
                    true);
            }
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "DisplaySessionTimeout();", true);
           
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900