1

im trying call a session variable stored in c#. My code is

var sesion_values= '<%= Session["userEmail"].ToString() %>';
console.log("sesion is: ", sesion_values);

this return:

sesion is:  <%= Session["userEmail"].ToString() %
4
  • Please don't add unrelated tags to your question (this problem doesn't involve jquery). Commented Apr 9, 2020 at 23:40
  • Use template string literals var session_values = `<% : ${Session["userEmail"].toString()} %>` Commented Apr 9, 2020 at 23:43
  • Do note that <%= ...> is a server side construct that should be gone (replaced by the value) by the time this line arrives at the browser. But for that it needs server-side processing in a .aspx or .ascx file. So where exactly do these lines of code live? Commented Apr 10, 2020 at 6:46
  • 1
    apparently is not run - probably you have it inside the .js file that is not been compiled by asp.net Commented Apr 10, 2020 at 8:31

1 Answer 1

1

From what I see the asp.net code is not run, so you probably place it inside the javascript file - where asp.net not compile it.

One trick is to add it on .aspx page (or on master page) just before you load the javascript file...

for example, inside the asp.net code do this.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        var sesion_values = '<%=Session["userEmail"].ToString()%>';
    </script>
    <script src="yourscript.js"></script>    
</head>

then you can use the sesion_values inside the javascript file.

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

Comments

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.