-2

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.

5
  • Are you tried RegisterStartupScript/RegisterClientScriptBlock already? How you're inject the script into ASPX page? Commented Dec 21, 2018 at 5:32
  • Yes. When i comment the below two lines code its show the alert box testing but its not working the sesindex varible set code. I dont know that is it right or wrong Commented Dec 21, 2018 at 5:35
  • well, the ["indexval"] should have thrown an exception. This version will work on aspx side of things where '<%=Session["indexval"]%>' will be rendered to something. Commented Dec 21, 2018 at 5:38
  • 1
    Possible duplicate of how to pass session value from code behind to javascript Commented Dec 21, 2018 at 5:42
  • No its different from that question. Because their they directly pass session value which is normally doing in asp.net i.e. string str = session["set"].ToString(). Here i want to set a value in javascript and its not working as per your answer Commented Dec 21, 2018 at 6:07

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

6 Comments

var sesVal = Session["indexval"]; Its not a proper way to set a value the cursor is not move after this line
Aha, sorry forgot to remove quotes around the variable name.
Thank u for your concern. But still its not working the cursor is not move after this line
Javascript not throwing any error when we write in code behind. I checked by debugger its directly break after var sesval = session["indexval"].ToString() line. Its not showing alert box
In aspx page we directly assign var session = '<%=Session["indexval"]%>' its gives proper result but in code behind how set var session as a session value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.