1

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. <% ... %>)"

Nope
  • 22,147
  • 7
  • 47
  • 72
ZSH
  • 631
  • 5
  • 16
  • 36
  • 1
    Similar issue discussed here: http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl – Riv Aug 19 '13 at 08:45
  • Tanks.I add 'Page.Header.DataBind()' in aspx and work fine :). – ZSH Aug 19 '13 at 09:20

1 Answers1

1

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"));
  });
AB Vyas
  • 2,349
  • 6
  • 26
  • 43
  • I add jquery.session.js and use $.session.get("myVar") but return 'undefined' – ZSH Aug 19 '13 at 08:56
  • I Update answer please check it. This can be possible with javascript also. simply write "<%= Session["test"]%>" – AB Vyas Aug 19 '13 at 09:24
  • tanks for answer but session set server side – ZSH Aug 19 '13 at 09:55
  • So now you can use javascript for that function getsession(){ var name = "<%= Session["test"]%>"; alert(name); }; This will work fine with me. if this will work for you please accept my answer. – AB Vyas Aug 19 '13 at 09:56
  • When i use "<%= Session["test"]%>" i get above error but i add Page.Header.DataBind in PageLoad event then "<%# Session["test"]%>" work fine – ZSH Aug 19 '13 at 10:18
  • you must try access session after set the session value. otherwise it will return undefined. – AB Vyas Aug 19 '13 at 10:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35741/discussion-between-ab-vyas-and-zsh) – AB Vyas Aug 19 '13 at 10:23