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

I have a code that doing the popup message for the session expired, as following. However, this code cannot do a correct counting session. When run the program, it will direct popup the session expired message which didn't follow the time that I had set. I know that why my message will popup so early because in my Page.ClientScript.RegisterStartupScript(cstype, csname, strconfirm, false);
I didn't add in the "timeout" so it is not executed as I want, but how to add it? when I try to add it in, it will result as input format error. Can someone assist me for this problem?


C#
string csname = "timeoutWarning";
 Type cstype = this.GetType();
 if (!Page.ClientScript.IsStartupScriptRegistered(cstype, csname))
 {
     var timeout = HttpContext.Current.Session.Timeout * 60 * 1000;
     string strconfirm = "<script>if(!window.confirm('Your login session is about to expire. Do you want to extend it?')){window.location.href='../login.aspx'}</script>";
     Page.ClientScript.RegisterStartupScript(cstype, csname, strconfirm, false);
 }


Thanks & regards,
NSP
Posted
Updated 2-Aug-12 17:31pm
v3

In Javasctipt you can do something like this..
JavaScript
var timeout = '<%= Session.Timeout * 60 * 1000 %>';
 var timer = setInterval(function() { timeout -= 1000; if (timeout == 1000) { clearInterval(timer); alert('Your session is about to expire!') } }, 1000);
 
Share this answer
 
sorry that I'm writing the code in the code behind, so how do I write the javascript that you provided into the cs format?
 
Share this answer
 
I have a solution for my question as following:

XML
string csname = "timeoutWarning";
           Type cstype = this.GetType();
           if (!Page.ClientScript.IsStartupScriptRegistered(cstype, csname))
           {
               var timeout = HttpContext.Current.Session.Timeout * 60 * 10000;
               //string strconfirm = ("<script>if(!window.confirm('Your login session is about to expire. Do you want to extend it?')){window.location.href='../login.aspx'}</script>");
               string strconfirm = "<script>" +
                   "window.setTimeout('SessionTimeOutHandler()', 10000);" +
                   "function SessionTimeOutHandler() { " +
                   "if(window.confirm('Your login session is about to expire' ))" +
                   "{window.location.href='../login.aspx'}" +
                   " } </script>";
               Page.ClientScript.RegisterStartupScript(cstype, csname, strconfirm, false);

           }


However, now I have a question that how to repeat shows the popup message when it is expired?
 
Share this answer
 
v3

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