简体   繁体   中英

How to get session value in Javascript but my javascript code is in the code behind in asp.net. I am trying to set the variable value in javascript

I am trying get the session value in javascript in my asp.net code behind. But the syntax is wrong I don't know how to get the session value. Here in my question i trying to set the value of sesindex variable which is a session value which i dont know how to set its totaly different

I tried to call javascript on page load normally its calling is fine. But when I am trying to set the variable value as session value its not calling alert box so their some where I put wrong but I don't know what it is.

Here is the code behind code:

str += "<script type='text/javascript'>" +
             //"alert('testing');" +
            "var sesindex = '<%=Session["indexval"]%>';" +
                "alert(sesindex);" +

                 "</script>";

I want to set sesindex value and show in alert box. I am trying to set the value but its not working. It is working fine in html page. But its not working in code behind.

Don't really like this but you can try this to get value into the string.

var sesVal = Session[indexval];

str += "<script type='text/javascript'>" +
        $"var sesindex = '{sesVal==null?"":sesVal.ToString()}'" +
            "alert(sesindex);" +"</script>";

This will get you covered when the value is null. You can read about $"{}" here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM