Click here to Skip to main content
14,490,855 members
Rate this:
Please Sign up or sign in to vote.
See more:
Actually i in my functionality i want to set the session value in javascript....i studied so many blogs about this. in that blogs said we can access but we can not assign. but in two blogs they said we can assign also but it is not working here below is the links where they said we can assign


http://stackoverflow.com/questions/15519454/how-can-i-access-session-variables-and-set-them-in-javascript

http://shekharshetemcts.wordpress.com/2013/11/27/how-to-access-session-variables-using-javascript-in-asp-net/comment-page-1/#comment-294​


Below is my javascriptcode Code.When i am using the above links code in my javascript code if condition i am getting an error

Error:-The Controls collection cannot be modified because the control contains code blocks (i.e. ).


<script type="text/javascript" >

function showorhide(id) {
    if (document.getElementById(id).style.display == "none") {
    document.getElementById(id).style.display = "block";
    var idA = id;
    if (idA == "workdiv3") {
    //Here i want to Assign the session["Diplay"]="Yes"
    }
    }
else {
    document.getElementById(id).style.display = "none";
    var idA = id;
    if (idA == "workdiv3") {
//Here i want to Assign the session["Diplay"]="No"
    }
}
}
</script>
Posted
Updated 24-Feb-14 19:44pm
v4
Comments
JoCodes 24-Feb-14 23:34pm
   
I tested your Sample link code , it works fine. Can you please post the Code which you tried?
ntitish 25-Feb-14 1:35am
   
Sir i update my javascript code......in my question

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

var id = "10";
'<%Session["x"] = "' + id +'"; %>' ;

alert('<%=Session["x"] %>');
   
Comments
ntitish 25-Feb-14 2:07am
   
If i am placing this code in my if condition the following error is coming
Error:-The Controls collection cannot be modified because the control contains code blocks (i.e. ).
Did i missed any thing.
k07 25-Feb-14 2:52am
   
hi..

From Where you passing this id . pls show that code
ntitish 25-Feb-14 4:27am
   
sir r u asking about javascript code ...............

<script type="text/javascript" >



function showorhide(id) {
if (document.getElementById(id).style.display == "none") {
document.getElementById(id).style.display = "block";
var idA = id;
if (idA == "workdiv3") {
var id = "10";
'<%Session["x"] = "' + id + '"; %>';

alert('<%=Session["x"] %>');
}
}
else {
document.getElementById(id).style.display = "none";
var idA = id;
if (idA == "workdiv3") {
var id = "11";
'<%Session["x"] = "' + id + '"; %>';

alert('<%=Session["x"] %>');
}
}
}
</script>
k07 25-Feb-14 6:20am
   
no....from where u call this JS fun ..in onclick=showorhide() ??
pls show that code..
ntitish 25-Feb-14 6:23am
   
sir insted of this session code i simply kept alert it is working 5n.....but there is problem in session tags because when i searched my error i found this link

http://www.aspdotnet-suresh.com/2011/12/aspnet-controls-collection-cannot-be.html
ntitish 25-Feb-14 6:23am
   
after changing according to the link also my same error is comming....
ntitish 26-Feb-14 5:11am
   
Sir did i confused you........because you stopped replying.....
k07 28-Feb-14 0:39am
   
no...I had an important function....so i didn't get time...
ntitish 25-Feb-14 4:37am
   
the respective error i am facing
Error:-The Controls collection cannot be modified because the control contains code blocks (i.e. ).
ntitish 25-Feb-14 6:20am
   
sir this was my last functionality have to complete.....so please try give reply fast sir if possible...........
k07 28-Feb-14 0:43am
   
error is occured due to many reasons....one is if you put this code in
<head> block then move them to the <body> block.....

can you show that full code ...JS+ remaining code section (fuul aspx page code)
ntitish 10-Mar-14 3:05am
   
it is to much code sir...if you want to see my code means i will send it by email if you say your email

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100