Reputation: 49
I have a form in Asp.Net with 3 pages by going with a next button to the next page. What I've done so far in C# is I created sessions like this:
Session["FirstName"] = txtFirst.Text;
Session["LastName"] = txtLast.Text;
Than what I did is on that Next button I called a javascript function where I tried to access these sessions like this:
<script type="text/javascript">
var fn = '<%=Session["FirstName"]%>';
var ln = '<%=Session["LastName"]%>';
</script>
But it's not working, when I am debugging it gets exactly what we entered inside quotes: http://prntscr.com/ag3wdo
Upvotes: 0
Views: 638
Reputation: 39284
That syntax: <%=Session["FirstName"]%>
is a special asp.net syntax. It needs to be processed on the server, where it gets replaced with the value of that session value. Only then does this get sent to the browser.
Some important notes:
<%= ... %>
syntaxUpvotes: 1
Reputation: 156978
Most likely those tags are in a .js
file, not an aspx
or ascx
page. Those tags only work on those extensions. Javascript files are sent as-is.
You can do a few things:
ashx
handler and replace the session tags by hand.Upvotes: 0
Reputation: 6348
You need to either have that script directly in an aspx page (not a .js file), or you need to set the values of the variables from a script in the aspx page.
Alternatively, you could send the js files to be evaluated by the .NET engine (create a handler), but that's not a good thing to do, as a general rule because it would process ALL js files and add overhead where you don't need it.
Last resort, you can set the values in a hidden field to then be accessed by javascript later. See Accessing Asp.Net Session variables in JS
Upvotes: 0