Solved

Session_OnEnd Event Not Firing

Posted on 2014-02-25
7
322 Views
Last Modified: 2014-02-26
We currently have a web app that times out after 20 minutes as intended. When this occurs I want to display a message box telling the user that the session has timed and out an to click OK to refresh the page.

I have the following VB code on the back end:

Imports System.Windows.Forms

Partial Class _Default
    Inherits System.Web.UI.Page
    Public Event OnEnd As EventHandler
    Public Property Timeout As Integer

    Public Sub Session_OnEnd()
        MessageBox.Show("Your session has timed out. Click OK to refresh your page.", _
            "Important:", _
             MessageBoxButtons.OK, _
             MessageBoxIcon.Exclamation, _
             MessageBoxDefaultButton.Button1)

        Dim result As Integer = MessageBox.Show("message", "caption", MessageBoxButtons.OK)
        If result = DialogResult.OK Then
            Response.Redirect("default.aspx")
        End If
    End Sub
End Class

And for testing purposes I’ve set the following entries in the web.config file to 1 minute for testing:

<sessionState mode="InProc" cookieless="false" timeout="1"/>

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="1"/>


The problem is that the event doesn’t seem to fire. I’m going into debug, executing one search and leaving the page alone, the nothing is happening. What am I missing here?

Thanks,

JB
0
Comment
Question by:JB4375
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39886151
0
 
LVL 1

Author Comment

by:JB4375
ID: 39886458
Interesting methods, but they seem a little over the top for what I'm looking for.

Also, from the code project link:
Note: In all the above scenarios, I am assuming SetTimeout method and session related variables will be reset whenever there is a post back. This may not work 100%, when there could be partial rendering and the SetTimeout method and session related variables may not be reset.

Isn't there a dependable method of simply capturing the event when it occurs, and then displaying a message box etc. as I've described above?
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39887327
The browser is independent of the server, and all communication between the two must be initiated by the browser. Hence, you cannot push a message box to the browser from the server. What I have done in the past is to add a <meta http-equiv="refresh"> tag to my page where the timeout value is set to be just a tad sooner than the actual session timeout value. This would perform an automatic refresh of the page. For your case, you wouldn't want this tag, but rather some Javascript to alert the user to the pending timeout. You could try doing something like (in your markup file):

<script>
    setTimeout(alertUserToTimeout, <%= Session.Timeout * 60000 %>);  <%-- Timeout is in minutes, so multiply by 60 for seconds, and by 1000 for milliseconds, which is what setTimeout expects --%>

    function alertUserToTimeout() {
        alert('Your session is about to expire!');
    }
</script>

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:JB4375
ID: 39887698
Right... I'm not concerned with alerting them before the session expires,

Just letting them know  when it has timed out so they aren't attempting to click around on the page before getting frustrated and then closing the window because they think something it wrong with the program.

I want the message box to inform them what has occurred, and clicking OK refreshes the page.

Questions:

In the code above, is session.timeout a system variable or can I just drop a number in there?

What would it look like for 20 minutes?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39887877
Session.Timeout is on the server, yes. It's value will be whatever you have configured it to be, the default being 20 minutes.

Yes, you can drop a number in there, but it would be better to use the variable so that the two values are in sync.

20 minutes would be: 20 x 60 x 1000 = 1200000
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39888351
If you want to push the message from the session_end event of the server to the client browser, you can use SignalR.

http://www.codeproject.com/Articles/526876/AddingplusSignalRplustoplusanplusASP-NetplusWebFor
0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 39888843
Thanks for the help!!

Much appreciated as always,

JB
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now