 |
|
|
For those of you using ASP.NET 2.0 with MS AJAX, here is an update for you. Some of you probably don't know that AsyncPostBacks (those caused by UpdatePanels and what not) cause you to do a little more work when registering scripts via the Server-Side. Also, keep in mind that this WILL WORK even when you don't have a ScriptManager on the Page.
Just change the path to your "Reconnect.aspx" page (see the bold text below) to reflect your site structure. I use this script in my MasterPage and call it each time the Page Loads. Always register this script everytime the page loads, it's smart enough to handle AsyncPostBacks and that's what you want.
/// <summary> /// Registers a Script with the Client-Side code to keep a user Session alive /// </summary> /// <remarks> /// Adapted from: http://www.codeproject.com/KB/session/Reconnect.aspx /// </remarks> private void RegisterSessionKeepAlive() { try { // NOTE: Everything needs converted to milliseconds int timeoutWindow = 30; // Number of seconds before session will timeout to fire a simulated PostBack to renew Session int timeout = (this.Session.Timeout * 60000) - (timeoutWindow * 1000); // if (System.Diagnostics.Debugger.IsAttached) timeout = 10000; // For Testing, comment if not needed string reconnectUrl = VirtualPathUtility.ToAbsolute("~/Common/SessionReconnect.aspx"); string startupScript = string.Format("window.setInterval('reconnectSession()', {0});\n", timeout); //Set to length required string scriptKey = "SessionReconnectScript"; string startupScriptKey = "SessionReconnectStartupScript";
ScriptManager sm = ScriptManager.GetCurrent(this.Page); if (sm != null && sm.IsInAsyncPostBack) ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), startupScriptKey, startupScript, true); else { if (!Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), scriptKey)) { System.Text.StringBuilder script = new System.Text.StringBuilder(); script.Append("var reconnectCount = 0;\n"); // Number of Reconnects script.Append("var reconnectMax = 5;\n"); // Maximum Reconnects script.Append("function reconnectSession(){\n"); script.Append("\treconnectCount++;\n"); script.Append("\tif (reconnectCount < reconnectMax){\n"); script.Append("\t\twindow.status = 'Session Refreshed ' + reconnectCount.toString() + ' time(s)';\n"); script.Append("\t\tvar img = new Image(1,1);\n"); script.AppendFormat("\t\timg.src = '{0}';\n", reconnectUrl); script.Append("\t}\n"); script.Append("}\n"); Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), scriptKey, script.ToString(), true); }
if (!Page.ClientScript.IsStartupScriptRegistered(this.Page.GetType(), startupScriptKey)) Page.ClientScript.RegisterStartupScript(this.Page.GetType(), startupScriptKey, startupScript, true); }
} catch (Exception) { throw; } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
For some reason the reconnect page still got cached at the client. I can see this with fiddler, I only see one request.
Setting the output cache didn't solve the problem. Therefore I did the following in the javascript and that worked: Reconnect.aspx?count=' + count
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
In my opinion (and this is just my opinion) using an ajax call to keep the session alive would be a more elegant solution.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Hi,
I have an ASP.net page that takes over an hour and sometimes more to execute. I have set the session timeout to 40 minutes and have the following java script to send mesg to server just to prevent timeout from occuring.
<script type="text/javascript" language="javascript"> function Reconnect(){ var img = new Image(1,1); img.src = "New.aspx"; } window.setInterval("Reconnect();", 600000); //Set to length required
</script> After the page has been running for more 60 minutes, I get a Page Cannot Be Displayed error. This error occurs in IE 6 , but the page executes without a problem in Firefox.
I would like to know what is actually causing the error and why is my script not working for IE. Is it an IE setting that can be changed or something else?
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Firstly let me say that a page request that takes an hour to process seems like it should be done in another way. My suggestion would be to use a background thread for processing with an AJAX timer request or auto page refresh to get status on the thread processing the request. You will see this type of page on travel sites when they request flight info for example.
this link has some info on asynchronous pages in asp.net http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/
Also see this code project article: http://www.codeproject.com/KB/aspnet/asynctransactionhandler.aspx[^]
you will also find many other examples if you google "ASP.NET background threads" or "ASP.NET asynchronous page requests".
The code you have used from my example for keeping session alive is not really any good if the browser is waiting for the complete response longer than the session timeout value as the javascript will not fire until the page is loaded.
I hope this helps.
Regards
Richard
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Is that the name of the page this code goes into? I'm guessing it is, but I would like to be sure.
Thanks much!
Been there, done that, forgot why!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Good grief! That was supposed to be what is Reconnect.aspx.
Now I finally got the structure of this solution.
However, I tried it and it prints the redirect, but it don't fix the problem.
This is in Visual Studio 2003, tested on an XP test server, using IIS V 6.
I've messed with every timing value that even remotely sounded like what I needed to try. I've tried everything I can find in goggle, This problem has continues for two weeks of frustrationf or me.
Such persistant problems are like a flame and the programmer is the moth (I'm the moth) that is held helpless in the fire by demands to pull this rabbit out of the hat. In this case, it's Microsoft told them it can be done, but it's me that has to implement what I begin to think is an impossible task.
Here is the code in the form I want to keep alive..
Any clues why this does NOT work?
Deadfrankenstein.aspx ....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.AddKeepAlive() ' keep alive End Sub
Private Sub AddKeepAlive()
Dim int_MilliSecondsTimeOut As Integer = (10 * 60000) - 30000 ' Refresh every 10 minutes. Dim str_Script As String = "<script type='text/javascript'> var count=0; " str_Script &= " var max = 7; " str_Script &= " function Reconnect(){" str_Script &= " " str_Script &= " count++; " str_Script &= " if (count < max) " str_Script &= " { " str_Script &= " window.status = 'Link to Server Refreshed ' + count.toString()+' time(s)' ; "
str_Script &= " var img = new Image(1,1); "
str_Script &= " img.src = '~/Reconnect.aspx'; "
str_Script &= " } " str_Script &= " } " str_Script &= " " str_Script &= " window.setInterval('Reconnect()'," & int_MilliSecondsTimeOut.ToString() str_Script &= " ); " str_Script &= "</script>"
Me.Page.RegisterClientScriptBlock("Reconnect", str_Script)
End Sub
This is the content of Reconnect.aspx ...
<%@ OutputCache Location="None" VaryByParam="None" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html> </html>
Been there, done that, forgot why!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I fixed the timout issue...
In the login script was code that was making a call to FormsAuthenticationTicket and the third parameter is the time from now for the cookie to expire. Editing that fixed my problem!
Been there, done that, forgot why!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
    Hai I want to develop a website, which would work like mail server . just like yahoo and google.com please help me in knowing what are the requirements and how it will work .
Yogendra Dubey
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Not sure about the relevance of your question here?
Good luck getting someone to do your work for you too.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
just posted this as a reply to a question on forums.asp.net.
This alternative is a lot smoother if you use Ajax but works without is well.
You need to use a master page and sliding session expiration, or at least it's a lot easier that way. Of course you could jam a timer control in every page, but master pages are pretty nice for this and for other stuff as well. Well, in my master I page add:
<asp:Timer ID="tmCheckStatus" Interval="1800000" runat="server">
Where the Interval (remember it's ms and not seconds) should be your session timeout minus at least 10 seconds. I prefer to go with 30 seconds to one minute before session expiration, depening on what I want to do when session is closing expiration. The 10 seconds is just because your session sometimes expires before the expiration you've set, butusually not ore than 10 seconds earlier. Do a little testing on this. Ok, now that I've got the timer control ticking, add a code behind to handle the tick of the timer. Protected Sub tmCheckStatus_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmCheckStatus.Tick If b2bGlobal.isUserOnline = False Then 'This is if you use forms authentication 'but I'd say it works equally fine for 'normal' sessions 'Or you can fire a popup or redirect to a page asking the users 'if they want to stay online FormsAuthentication.SignOut() FormsAuthentication.RedirectToLoginPage() End If End SubWell, that's how I solve all annoying session timeouts and it's really easy. You can even set the timer interval to be (your session timeout value)/6 or some other number. For each tick you can change a picture or something like that, showing the ammount of time the user has left before automatic redirection occurs. It's nice for them and a cool feature for oneself, although one should take care not to let this sucker tick to much, as it could eat up server resources.
www.pixer.se
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Thanks for sharing but sorry Timer control cant be used without AJAX support installed. So it will be a lot of work on all fronts, testing/deployment/configuration, for AJAX overhead if its not there already.
Thanks
Abbas
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
By using this alternative it's quite complex to set the maximum number of times the session is allowed to be refreshed. It's easier to do this client side (as shown in this article). If you use Timer you will probably do this server side and as said: it's quite complex. Doing everthing client side is mich easier.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello there,
Just wanted to say thank you! This save me a whole bunch of time. Also just cant believe that somebody would even dare to call you "stupid" how utterly rude! I am stunned and shocked.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Message Automatically Removed
|
|
|
|
 |
|
|
I don't usually answer to anything but you deserve it. You are rude and have no idea what you are talking about. Your profile shows you are just a looser. There are many ways to write a piece of code and there is nothing wrong with this one, it does not make a request to the server until the session is about to expire, not all the time. Besides you can code it only to happen when the user is in the middle of a data entry form. Thanks for the article and we can do without comments like this.
|
| Sign In·View Thread·PermaLink | 4.37/5 (10 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Finally I figured this out thanks to your refresh.aspx trick along with adding the date parameter to force the server hit. I had been trying to just fetch an aspx page and that wasn't working. I do mine on the client side and here's my javascript for it if anyone's interested. I am refreshing every 15 minutes and my web.config is set at the default 20 minute timeout.
- Brian
<script language="JavaScript"> <!-- // Prevents session timeout var timer = setTimeout('refresh()', 30000); function refresh() { var xmlHttp; if (window.ActiveXObject) { var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else { var xmlHttp = new XMLHttpRequest(); }
xmlHttp.open("GET", "http://www.somehost.com/Refresh.aspx", false); xmlHttp.send();
timer = setTimeout('refresh()', 30000); } //--> </script>
|
| Sign In·View Thread·PermaLink | 4.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Usually about 20 mins but session timeout can be set in the web.config:
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20" />
|
| Sign In·View Thread·PermaLink | 3.25/5 (3 votes) |
|
|
|
 |