2013-08-19 34 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