2013-08-19 1 views
1

Я хочу получить сессию в jQuery (v 1.9.1).Получить вопрос сеанса

Я использовал следующий код:

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

Но я получаю следующее сообщение об ошибке:

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

+1

Аналогичный вопрос обсуждался здесь: http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl – Riv

+0

Tanks.I add 'Page. Header.DataBind() в aspx и отлично работает :). – ZSH

ответ

2

Попробуйте

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

ИЛИ вы можете попробовать это

// To Read 
    $(function() { 
    //Set the value to session 
    $.session.set("userName", $("#uname").val()); 
    //Get the value to session 
    alert($.session.get("myVar")); 
    }); 
+0

Я добавляю jquery.session.js и использую $ .session.get ("myVar"), но возвращаю 'undefined' – ZSH

+0

Я обновляю ответ, пожалуйста, проверьте его. Это также возможно с помощью javascript. просто напишите «<% = Session [" test "]%>" –

+0

танки для ответа, но серверная сторона набора сеансов – ZSH