title: Handle session timeout in asp.net using Javascript
body: <p>Essentially I want to be able to catch when a user lets their session timeout and then clicks on something that ends up causing an Async postback. I figured out that if I put this code in my Session_Start (in Global.asax) then I can catch a postback that occurred during a session timeout:</p>
  
  <pre><code>    With HttpContext.Current
          If TypeOf .Handler Is Page Then
              Dim page As Page = CType(.Handler, Page)
              If page IsNot Nothing AndAlso page.IsPostBack Then
                  'Session timeout
              End If
          End If
      End With
  </code></pre>
  
  <p>This works fine. My question is, I would like to be able to inject some javascript into the Response and then call Response.End() so that the rest of the application does not get finish executing. The problem is that when I try <code>Response.Write("&lt;script ... ")</code> followed by <code>Response.End()</code> then javascript does not get written to the response stream. I'm sure there are other places in the application that I can safely write Javascript to the Response but I can't let the rest of the application execute because it will error when it tries to access the session objects.</p>
  
  <p>To sum up: I need to inject javascript into the response in the Session_Start event in Global.asax</p>
  
  <p>Note: You may be wondering why I'm not doing this in Session_End...we don't use InProc sessions and so Session_End doesn't get called...but that's beside the point...just wanted to make it clear why I'm doing this in Session_Start.</p>