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
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