Why Doesn't This Work?

J

Joey

I am working on an asp.net 1.1 web app in C#. I downloaded some sample
code that is supposed to allow for me to persist session state. The
code is as follows:

private void PersistSessionState()
{
int MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000;
string Script = @"
<script type='text/javascript'>
var Count=0;
var Max = 1000;
function PersistSessionState()
{
Count++;
if (Count<Max)
{
window.status = 'Link to server refreshed ' +
Count.toString()+' time(s)';
var Img = new Image(1,1);
Img.src = '/PersistSessionState.aspx';
}
}
window.setInterval('PersistSessionState()'," +
MilliSecondsTimeOut.ToString()+ @");
</script>
";

this.Page.RegisterClientScriptBlock("PersistSessionState", Script);
}

When I put this into my app, I did the following:

1. I added this function to the code behind page.
2. I added the line "this.PersistSessionState();" to call the function
from the Page_Load event handler.
3. I added "<%@ OutputCache Location='None' VaryByParam='None' %>" at
the top of the aspx page, to control the client-side caching.
4. I set the session timeout value to "1" in the web.config file.

Now, when I run this, (predictably) after about thirty seconds, the
status bar reads "Link to server refreshed 1 time(s)". As you can see
in the javascript code, this fires about thirty seconds before the
Session times out. So, when I see the message and wait about 45
seconds or so after, if I try to click another page on the site, I get
a "Server Error in '/' Application - Object reference no set to an
instance of an object" message. This happens because the solution did
not work - the session expired.

So what is wrong with this solution? Why doesn't it work? How can I
fix it?

Thanks in advance for your help.

JP
 
B

bruce barker

could be caching, try:


Img.src = '/PersistSessionState.aspx?c=' + Count;


bruce (sqlwork.com)
 
G

Guest

It appears MilliSecondsTimeOut is the object not set to instance - there
doesn't seem to be any declaration for the object (in the script block), and
you are attempting to access a "ToString()" method at the end of the script.
 
G

Guest

could be caching, try:

Img.src = '/PersistSessionState.aspx?c=' + Count;

bruce (sqlwork.com)












- Show quoted text -

OR

Img.src = '/PersistSessionState.aspx?' + new Date().getTime()

also read this article about preload via js
http://www.webreference.com/programming/javascript/gr/column3/

or use XMLHTTP

var page_request = false;
page_request = new ActiveXObject('Msxml2.XMLHTTP');
page_request.open('GET', '/PersistSessionState.aspx?' + new
Date().getTime(), true);
page_request.send(null);
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top