Re: Hyperlinking and ASP.NEt Session

N

Nedu N

Thanks Mun.
I was trying to use Timer object for Session Timeout popup and redirect
already, but it was not not working...
would you give me exaple of how to code time controls to popup a message and
redirect to login page when the session times out..
(using Session.Timeout property to determine the session timout).

Thanks
Nedu
 
M

MSFT

M

Munsifali Rashid

What you need to do, is start a javascript timer on each page load, and then
update the timer every second until the session expires. If the user
navigates to a different page, the clock will reset as they are actively
using the session.

Create a timers javascript file and put the following two functions in it.
This script must be referenced in every page on which you want to show the
popup. I.e. save it as "includes/timers.js" and then each page must
reference it using <script language="JavaScript"
src="includes/timers.js"></script>.


function startClock()
{
dWatch = 0;
dStarted = new Date();
}
function updateClock(iTimeOutAlert)
{
setTimeout("updateClock('" + iTimeOutAlert + "');", 100);
dNow = new Date();
dWatch = dNow.getTime() - dStarted.getTime();
dClock = Math.round(dWatch/1000);
if (dClock == iTimeOutAlert)
{
alert("Warning!\n\nYour session has expired.\nRedirecting to login
page...");
window.location.href = "loginpage.aspx";
}
}


Once this is done, you need to modify pages using the alert to call
startClock() and updateClock(). You can do this using the following (in the
Page_Load function):


[C#]
int intTimeOut = Session.Timeout*100;
string strTimerScript = "startClock();updateClock(" + intTimeOut.ToString()
+ ");"
Page.RegisterStartupScript("timerScript", strTimerScript);


[VB.NET]
Dim intTimeOut as Integer = Session.Timeout*100
Dim strTimerScript As String = "startClock();updateClock(" &
intTimeOut.ToString() & ");"
Page.RegisterStartupScript("timerScript", strTimerScript)


This code sample is constructed partly from memory, and partly from some
code snippets I had floating around. Haven't used it for a coupla years
now, though it worked perfectly back then, but it was being used on a
classic ASP website, not .NET. That shouldn't be an issue though, as it's
all client-side code. If there's any problems, let me know, and I'll try
and help :)

Regards,

Mun
 
N

Nedu N

Thanks Mun...
I tried but i am make it working...i just tried with the script that you
gave me..

I created a js file in my project itself - TimeoutTimer.js
and i referenced this on one of my page's HTML as reference it using <script
language="JavaScript"
src="TimeoutTimer.js"></script>.

and i had following code in my page load
Session.Timeout = 10; //i am ust overriding this...

int intTimeOut = Session.Timeout * 100;

Global.strScript = "startClock();updateClock(" + intTimeOut.ToString() + ");

Page.RegisterStartupScript("timerScript", Global.strScript);



But the output that i am getting is the following displayed on my page as a
string...i think the the javascript was not executed...woudl u please tell
me what is the problem since i am new to scripts..

startClock();updateClock(1000)





Munsifali Rashid said:
What you need to do, is start a javascript timer on each page load, and then
update the timer every second until the session expires. If the user
navigates to a different page, the clock will reset as they are actively
using the session.

Create a timers javascript file and put the following two functions in it.
This script must be referenced in every page on which you want to show the
popup. I.e. save it as "includes/timers.js" and then each page must
reference it using <script language="JavaScript"
src="includes/timers.js"></script>.


function startClock()
{
dWatch = 0;
dStarted = new Date();
}
function updateClock(iTimeOutAlert)
{
setTimeout("updateClock('" + iTimeOutAlert + "');", 100);
dNow = new Date();
dWatch = dNow.getTime() - dStarted.getTime();
dClock = Math.round(dWatch/1000);
if (dClock == iTimeOutAlert)
{
alert("Warning!\n\nYour session has expired.\nRedirecting to login
page...");
window.location.href = "loginpage.aspx";
}
}


Once this is done, you need to modify pages using the alert to call
startClock() and updateClock(). You can do this using the following (in the
Page_Load function):


[C#]
int intTimeOut = Session.Timeout*100;
string strTimerScript = "startClock();updateClock(" + intTimeOut.ToString()
+ ");"
Page.RegisterStartupScript("timerScript", strTimerScript);


[VB.NET]
Dim intTimeOut as Integer = Session.Timeout*100
Dim strTimerScript As String = "startClock();updateClock(" &
intTimeOut.ToString() & ");"
Page.RegisterStartupScript("timerScript", strTimerScript)


This code sample is constructed partly from memory, and partly from some
code snippets I had floating around. Haven't used it for a coupla years
now, though it worked perfectly back then, but it was being used on a
classic ASP website, not .NET. That shouldn't be an issue though, as it's
all client-side code. If there's any problems, let me know, and I'll try
and help :)

Regards,

Mun



Nedu N said:
Thanks Mun.
I was trying to use Timer object for Session Timeout popup and redirect
already, but it was not not working...
would you give me exaple of how to code time controls to popup a message and
redirect to login page when the session times out..
(using Session.Timeout property to determine the session timout).

Thanks
Nedu
 
M

Munsifali Rashid

There's an error in the code - I forgot to wrap the javascript in <script>
tags.

Global.strScript = "startClock();updateClock(" + intTimeOut.ToString() + ");

Should be:

Global.strScript = "<script language='JavaScript'>startClock();updateClock("
+ intTimeOut.ToString() + ");</script>";

Also, the number passed in should be the Session.Timeout in seconds, so
intTimeOut should be:

int intTimeOut = Session.Timeout * 60;

I've just tested this code, and it seems to be working.

Hope this helps,

Mun
 
Joined
Sep 7, 2006
Messages
2
Reaction score
0
Session Expire Modal Popup

hi friends

I want to show a modal popup to user before 1 mintue of session expiration in asp.net. I want it in my aspx page.
If anyone knows, then please repy immediately.

Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top