1

How can I set a JavaScript variable to an ASP.NET Session? Currently the value in the price is not stored in the session, instead it stores the string "'+price+'". How to store the price variable's value?

function getCheckboxVal(el)
{
    var price = el.getAttribute("Price");
    '<%Session["Price"] = "' + price +'"; %>';
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
batwing
  • 257
  • 1
  • 8
  • 22
  • Why using JS to set that session variable? – jazb Nov 22 '18 at 05:59
  • 1
    Session variables are set and maintained on the server. You cannot access them directly with JavaScript. To set / get the value of a Session variable in JavaScript you would need to use an AJAX call to the server to a script that sets / gets the value and returns it to the JavaScript code. – jazb Nov 22 '18 at 06:00
  • no its not @JohnB you can access it in the javascript – Hamza Haider Nov 22 '18 at 06:01
  • @HamzaHaider just to confirm are you saying it is possible to access session variables `directly` from javascript? – jazb Nov 22 '18 at 06:05
  • yes it is. We don't need to send ajax call – Hamza Haider Nov 22 '18 at 06:07
  • 2
    Who have confusion Please read this https://stackoverflow.com/questions/15519454/how-to-access-session-variables-and-set-them-in-javascript – Hamza Haider Nov 22 '18 at 06:07
  • @HamzaHaider - i'd be keen to see a sample of that - got a link to example? – jazb Nov 22 '18 at 06:07
  • @HamzaHaider just seen your link. if you read it carefully it supports `my` argument. you are left where you started - wrong. – jazb Nov 22 '18 at 06:10
  • @JohnB You cannot access them directly with JavaScript. To set / get the value of a Session variable in JavaScript you would need to use an AJAX call to the server to a script that sets / gets the value and returns it to the JavaScript code... This is wrong statement according to the link – Hamza Haider Nov 22 '18 at 06:11
  • @HamzaHaider - no the answer is wrong and others comment to that affect. someone has just answered below to also that same affect. read this carefully - `You can not directly set session from JS because session is stored on server and js is client side`. this is a fact - sorry to disagree but i'm surprised people get so confused over client-side and server-side. – jazb Nov 22 '18 at 06:14
  • @JohnB It is astonishing for me that you talked about ajax call and now you are changed – Hamza Haider Nov 22 '18 at 06:17
  • 2
    ajax is a way to set something server side. the answer below is using another technique to set values on controls. none of this detracts from the fact you were incorrect. trying to deflect the argument onto something else is futile. i stand by my first comment `Session variables are set and maintained on the server. You cannot access them directly with JavaScript. ` – jazb Nov 22 '18 at 06:20
  • First the server works on that text (it is not seen as JavaScript yet) and processes those ‘<% >‘ parts. *Then* it is sent to the server and treated as JavaScript. Inspect the source in the browser to see – Hans Kesting Nov 22 '18 at 06:37
  • @HansKesting - sorry hard to follow you but all good – jazb Nov 22 '18 at 06:45

3 Answers3

6

You can not directly set session from JS because session is stored on server and JS runs at client side.

You can use hidden inputs for this purpose like:

<input  type='hidden' id='Price' name='Price'/>

var price = el.getAttribute("Price"); //Populate price var as required
var h=document.getElementById('Price'); 
h.value=price; //set hidden input's value

Then get this var price to set the session from code behind like:

Session["TestSession"] = Request.Form["Price"];
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
4

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

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

We cannot directly set clientside value to session. We can do this by using Hidden fileds

<asp:HiddenFieldID="hdPrice" runat="server" /> 

Further get this js price and set to variable hprice in javascript. This variable can be added into session

Session["PriceData"] = hdPrice;

Hope this helps you.