Question

I want to get session in jQuery(v 1.9.1).

I used the following code:

<script>
    var testUser = '<%= Session["User"] %>';
    alert(testUser);
</script>

But I get the following error:

"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."

"Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)"

Was it helpful?

Solution

Try this

<script type="text/javascript">
    function getsession(){    
        var name = "<%= Session["test"]%>";
        alert('Session value = >' + name );
    };
</script>

OR you can try this

  // To Read
  $(function() {
  //Set the value to session
  $.session.set("userName", $("#uname").val());
  //Get the value to session
  alert($.session.get("myVar"));
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top