Since Session
state maintained on server-side, it can't be assigned directly from client-side. You can perform an AJAX callback to a server-side web method which later stores session state value:
[WebMethod(EnableSession = true)]
public void SetPrice(string value)
{
if (Session != null)
{
Session["Price"] = value;
}
}
function getCheckboxVal(el) {
var price = el.getAttribute("Price");
$.ajax({
type: 'POST',
url: 'Page.aspx/SetPrice',
data: { value: price },
success: function (data) {
// do something
}
// other AJAX settings
});
}
Or using hidden field with runat="server"
attribute and assign its value to bring it into code-behind:
ASPX
<asp:HiddenField ID="HiddenPrice" runat="server" />
<script>
function getCheckboxVal(el) {
var price = el.getAttribute("Price");
document.getElementById('<%= HiddenPrice.ClientID %>').value = price;
}
</script>
Code behind
Session["Price"] = HiddenPrice.Value.ToString();
Reference: Accessing & Modifying Session from Client-Side using JQuery & AJAX in ASP.NET