To update the session you need to make a call - I suggest a simple handler that just shows an empty gif. Here is a simple code for this:
public class JustEmptyGif : IHttpHandler ,IRequiresSessionState
{
private readonly byte[] GifData = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b
};
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "image/gif";
context.Response.Buffer = false;
context.Response.OutputStream.Write(GifData, 0, GifData.Length);
}
public bool IsReusable
{
get {
return false;
}
}
}
This code is just a handler, let’s say EmptyImage.ashxand note that I have included IRequiresSessionStatewhich make it invoke and update the session.
Now all you have to do is update the hidden image using a script like:
<img id="keepAliveIMG" width="1" height="1" alt="" src="EmptyImage.ashx?" />
<script>
var myImg = document.getElementById("keepAliveIMG");
myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
</script>
I put a random number at the end to avoid caching it and making it reload it again. There are no messages, only a small image download.