Skip to content Skip to sidebar Skip to footer

Maintaining Page Number In Dropdownlist After Postback To Refresh Server Timeout

I'm using .Net, VB and webforms. I have a timer that pops up to allow the user to refresh their connection before the session expires. Three of the pages in the app use the exact

Solution 1:

You don't need to refresh the page to keep the session active. Instead, you can just use AJAX to call a method in an .asmx file which has EnableSession:=True.

An example of the code for that:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
PublicClass SessionKeepAlive
    Inherits System.Web.Services.WebService

    ' this function is to called every 19 minutes from the Master page' to keep the session alive.
    <WebMethod(EnableSession:=True)> _
    <Script.Services.ScriptMethod(UseHttpGet:=True)> _
    PublicFunction Check() AsStringIf HttpContext.Current.Session("username") IsNothingThenReturn"expired"ElseReturn"ok"EndIfEndFunctionEndClass

And if you were using an <asp:ScriptManager>, the JavaScript for the AJAX could look like:

<scripttype="text/javascript">//<![CDATA[                                                                                           var keepAlive = function() {                                                                          
        Sys.Net.WebServiceProxy.invoke("SessionKeepAlive.asmx", "Check", true, {}, SessionKeepAlive_Callback);
    }                                                                                                     

    functionSessionKeepAlive_Callback(result, eventArgs) {                                               
        if (result != 'ok') {                                                                         
            alert('Your session has timed out.\nPlease log in again.');                           
            window.location = 'login.aspx';                                                       
        };                                                                                            
    }                                                                                                     
    // Set 19 minute .NET session keep alive timer...                                                     window.setInterval(keepAlive, 19 * 60 * 1000);                                                        
    //]]>                                                                                                 </script>

You might choose a different way of calling the method in the web service, e.g. by using jQuery.

Post a Comment for "Maintaining Page Number In Dropdownlist After Postback To Refresh Server Timeout"