I assume you are using ASP.NET MVC (C#), if not then this answer is not in your issue.
Is it impossible to assign session values directly through javascript? No, there is a way to do that.
Look again your code:
<script type="text/javascript" >
{
Session["controlID"] ="This is my session";
}
</script>
A little bit change:
<script type="text/javascript" >
{
<%Session["controlID"] = "This is my session";%>
}
</script>
The session "controlID" has created, but what about the value of session? is that persistent or can changeable via javascript?
Let change a little bit more:
<script type="text/javascript" >
{
var strTest = "This is my session";
<%Session["controlID"] = "'+ strTest +'";%>
}
</script>
The session is created, but the value inside of session will be "'+ strTest +'" but not "This is my session". If you try to write variable directly into server code like:
<%Session["controlID"] = strTest;%>
Then an error will occur in you page "There is no parameter strTest in current context...". Until now it is still seem impossible to assign session values directly through javascript.
Now I move to a new way. Using WebMethod at code behind to do that. Look again your code with a little bit change:
<script type="text/javascript" >
{
var strTest = "This is my session";
PageMethods.CreateSessionViaJavascript(strTest);
}
</script>
In code-behind page. I create a WebMethod:
[System.Web.Services.WebMethod]
public static string CreateSessionViaJavascript(string strTest)
{
Page objp = new Page();
objp.Session["controlID"] = strTest;
return strTest;
}
After call the web method to create a session from javascript. The session "controlID" will has value "This is my session".
If you use the way I have explained, then please add this block of code inside form tag of your aspx page. The code help to enable page methods.
<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
Source: JavaScript - How to Set values to Session in Javascript
Happy codding,
Tri