Here is the PROBLEM:
I have created online loan applying - web based application. It was consisted of almost 100 fields to be filled up. They don’t want wizard based process, they don’t want tab based process during applying, they simply want flat application as is in paper version.
If you are IT professional you need more than 5 minutes to fulfill the application. But there is one BUT… what about the others?!?!?!
They need more then 20 minutes. And here is a problem..After reaching this session limit and trying to submit application, background session variables are reset and you need to insert information from the beginning.
This is specific case, but you, as a developer you need to provide application that can satisfy all users needs (how pretty this sounds :).
Here is the SOLUTION:
There are more than one solutions, but I have chosen this one:
Using jQuery as a powerful library for Ajax, my Idea is to create one dummy page and to make “post” or “get” requests every X seconds using jQuery functions.
For this purpose I have created webcontrol (with embedded Java Scripts and whole functionality) that you just need to reference to your project, set 2 properties and you have endless session :)
Step 1: Creating WebControl
As you see at the picture bellow, My SessionRefresher webcontrol is consisted of jquery library, custom JavaScript functions and one class that has webcontrol interface implementation
SessionRefresher class has definition of two public properties
- SessionUrl: Url where get web request will be make. If you don’t define it, the default value is: SessionPost.aspx
- TimeOutPeriod: Period between two requests (in seconds). The default value is 15 seconds.
Custom JavaScript file has 3 functions:
One for delaying request, one for making request, and one for creating Guid. You are asking why Guid?!?!? Because of the get method. If once “get” request is fired using one Uri, next request should be different Uri because the first one is already cached and equal Uri will not produce request. Therefore I have added one “dummy” QueryString argument “id” and at every call i am assigning different (generated) Guid.
function
MakeRequest() {
if ((sesReqUrl != null)) {
var Ans = (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase();
$.get(sesReqUrl+"?id=" + Ans, function() { Delay(); });
}
}
Only you should need to do is on Init of web control, to register javascript files, and to expose to javascript world these previously defined properties: SessionUrl and TimeOutPeriod.
ClientScript.RegisterClientScriptBlock(GetType(), "sesReqUrl", "var sesReqUrl='" + SessionUrl + "';", true);
ClientScript.RegisterClientScriptBlock(GetType(), "sesReqTime", "var sesReqTime=" + TimeOutPeriod + ";", true);
ClientScript.RegisterClientScriptResource(GetType(), "SessionRefresherCtrl.jquery-1.3.2.min.js");
ClientScript.RegisterClientScriptResource(GetType(), "SessionRefresherCtrl.JScript.js");
Step 2: Using SessionRefresher webcontrol
After I have created sessionRefresher webcontrol, i need to use it on my project.
It is so easy…here are the steps
2.1. Just reference to your web project.
2.2. Register on your page
<%@ Register Assembly="SessionRefresherCtrl" Namespace="SessionRefresherCtrl" TagPrefix="cc1" %>
2.3. Define instance of web control
2.4. Set the properties
2.5. Create post page
In this case it is post.aspx, and I have added debug console action, i.e. output the time every time the page is requested.
2.6. Enjoy !!!
Here is the result of previously described steps:
As you see, post.aspx page was requested every 3 seconds because of the timeout period configuration.
CONCLUSION
Yup, it is just that easy.
This is something that may or may not be useful to a lot of people, depending on you situation, but this is for applications that may have a lot of idle time and users are annoyed with their Sessions dying... considering most users don't know what a Session is, but they know that the application stops working correctly.
Since this is all done via a jQuery ajax call, there is no evidence to the user this is even happening, no flicker or popup, nothing.