locked
Access session variable in javascript RRS feed

  • Question

  • User754342614 posted

    I'm trying to detect when a user leaves after making a change without recalculating.  I'm setting a session variable whenever they update and resetting it when they calculate.  The updating and recalculating are done without a postback so I cannot use a hidden field.  On my page I have:

    window.onbeforeunload = confirmExit;

    In my code I have:

    Dim csname As String = "csconfirmExit"

    Dim cstype As Type = Me.GetType()

    Dim cs As ClientScriptManager = Page.ClientScript

    If (Not cs.IsClientScriptBlockRegistered(cstype, csname)) Then

    Dim cstext2 As New StringBuilder()

    cstext2.Append("<script type=""text/javascript""> function confirmExit() {")

    cstext2.Append(" var a; a='<%= Session(""DetailsModified"") %>'; if (a == ""True"")")

    cstext2.Append(" return ""Recalculate"" } </")

    cstext2.Append("script>")

    cs.RegisterClientScriptBlock(cstype, csname, cstext2.ToString)

    End If

     

    The problem is a is not being evaluated, it contains the literal '<%= Session("DetailsModified")%>

    I cannot evaluate the session variable when the page loads because as I said the updates take place without a postback.  How can I evaluate the session variable dynamically through javascript?

     

    Thanks!

    Wednesday, February 22, 2012 11:39 AM

Answers

  • User1943143334 posted

    Hi,

    Try the following code!

    Dim csname As String = "csconfirmExit"
    Dim strSessionValue As String = Session("DetailsModified").ToString()
    
    Dim cstype As Type = Me.[GetType]()
    
    
    Dim cs As ClientScriptManager = Page.ClientScript
    
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname)) Then
    
    
    	Dim cstext2 As New StringBuilder()
    
    	cstext2.Append("<script type=""text/javascript""> function confirmExit() {")
    
    	cstext2.Append(" var a; a='" + strSessionValue + "'; if (a == ""True"")")
    
    	cstext2.Append(" return ""Recalculate"" } </")
    
    	cstext2.Append("script>")
    
    
    
    	cs.RegisterClientScriptBlock(cstype, csname, cstext2.ToString)
    End If
    

    Hope it helps u...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2012 8:45 AM

All replies

  • User1618479829 posted

    Assign Session(""DetailsModified"") value to a string and pass the string to Java script .

    Wednesday, February 22, 2012 11:52 AM
  • User754342614 posted

    Where would I do the assigning?  I can't do it when I register the script or when I load the page.

    Wednesday, February 22, 2012 12:19 PM
  • User-768391503 posted

    How would you read it on the server? Do that and append it to the string!

    Eric

    Wednesday, February 22, 2012 10:42 PM
  • User-996336415 posted

    You can include at the end of your .aspx page (just before </body>) the following script block. You will get confirm dialog box when user try to close browser's window if session variable haz value "True". I think, this is thet you need.

    <script>
    var a='<%= Session["DetailsModified"] %>';

    window.onbeforeunload = function () {
        if (a == "True") return "Recalculate";
    }
    </script>


    O. Zhelezov

    Thursday, February 23, 2012 8:40 AM
  • User1943143334 posted

    Hi,

    Try the following code!

    Dim csname As String = "csconfirmExit"
    Dim strSessionValue As String = Session("DetailsModified").ToString()
    
    Dim cstype As Type = Me.[GetType]()
    
    
    Dim cs As ClientScriptManager = Page.ClientScript
    
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname)) Then
    
    
    	Dim cstext2 As New StringBuilder()
    
    	cstext2.Append("<script type=""text/javascript""> function confirmExit() {")
    
    	cstext2.Append(" var a; a='" + strSessionValue + "'; if (a == ""True"")")
    
    	cstext2.Append(" return ""Recalculate"" } </")
    
    	cstext2.Append("script>")
    
    
    
    	cs.RegisterClientScriptBlock(cstype, csname, cstext2.ToString)
    End If
    

    Hope it helps u...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2012 8:45 AM