Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I have Session and a Textbox, I want to assign the value that Session is holding to Textbox?
I tried to get the value in a Variable in JQuery as below
var a='<%=Session["TireLn"]%>';


But in a I don't have actual value of Session. Instead it shows a as
'<%=Session["TireLn"]%>'


My
'<%=Session["TireLn"]%>'
has a value "CARGO G26"

Can anyone help me in assigning a session value to Textbox?
Posted
Comments
Modi Mayank 10-Mar-15 23:52pm    
What are you using MVC or simple webforms? Is there any error?
sudevsu 11-Mar-15 8:34am    
simple Web forms. Not MVC. No errors. but session value is coming into Var a
instead a is taking that as string and display '<%=Session["TireLn"]%>'

If this code is in a js file then it won't work, that code has to be on an aspx page. You can define the var on the aspx page and use it in the js file though.

in aspx;

JavaScript
<script>
var myVar = '<%=Session["sessionVar"]%>';
</script>


in your js;

JavaScript
alert (myVar);


Make sure you don't try and use the var before it is defined though.
 
Share this answer
 
As per your comment, I tried to run a simple test to check it out but it's working in my local. I tried:

1. My code-behind is something like:
C#
protected void Page_Load(object sender, EventArgs e)
{
   Session["TireLn"] = "test";
}


2. Accessing value as:
JavaScript
<script type="text/javascript">
      $(document).ready(function () {
         var a = '<%=Session["TireLn"]%>';
         alert(a);
      }
</script>


3. In Result, I'm getting "test" in alert on page load.

I think, you are doing something wrong in your code. Consider and check your code with above one. If you still having issue, provide important code that you are using here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900