admin管理员组

文章数量:1023263

I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.

I can assign strings to these Session variables but i cannot assign values from Form elements to these variables

function SetUserName() 
{       
     <% var text1= "Charles";%>
     <%Session["UserName"] =text1;%>
     var session_value='<%=Session["UserName"]%>';
}
</script>

This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.

function LoadUserName()
{
    var username = '<%= Session["UserName"] %>';
    alert(username);
}

This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?

For Example Say i have a Text Box

 <asp:TextBox ID="Address_Box" runat="server"  TextMode="MultiLine"
             MaxLength="200" ></asp:TextBox>

How can I send the text from this box to the session variable?

I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.

I can assign strings to these Session variables but i cannot assign values from Form elements to these variables

function SetUserName() 
{       
     <% var text1= "Charles";%>
     <%Session["UserName"] =text1;%>
     var session_value='<%=Session["UserName"]%>';
}
</script>

This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.

function LoadUserName()
{
    var username = '<%= Session["UserName"] %>';
    alert(username);
}

This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?

For Example Say i have a Text Box

 <asp:TextBox ID="Address_Box" runat="server"  TextMode="MultiLine"
             MaxLength="200" ></asp:TextBox>

How can I send the text from this box to the session variable?

Share Improve this question edited Jun 5, 2014 at 9:35 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Jun 5, 2014 at 9:27 MarsOneMarsOne 2,1865 gold badges30 silver badges54 bronze badges 6
  • 1 javascript directly can not "directly" set backend session variable. – Sandeeproop Commented Jun 5, 2014 at 9:31
  • JavaScript does not have sessions. ASP does. – kraxor Commented Jun 5, 2014 at 9:31
  • Not knowing something doesn't get downvote - I corrected. He's here because he doesn't know – Ali Habibzadeh Commented Jun 5, 2014 at 9:34
  • You can use cookies or HTML5 local storage for storing form values. But notice that such data are stored on client's machine which can be wiped away ! – Rahul Gupta Commented Jun 5, 2014 at 9:35
  • Thnks Xgreen, But is it not possible to assign a HTML element value to these variable instead of "Charles". Till now it works fine... – MarsOne Commented Jun 5, 2014 at 9:35
 |  Show 1 more ment

2 Answers 2

Reset to default 1

Your code:

var session_value='<%=Session["UserName"]%>';

does not "access the ASP.Net session from javascript".

With this code you do generate (during the processing of the request) some text that will be sent to the browser. This text contains the string that results from the expression Session["UserName"].

Once this text arrives at the browser it is interpreted as javascript. This javascript has no knowledge of any session values or the fact that it was (partly) generated.

You need to find some way to send data from the browser back to the server (using postback or ajax, for instance) before it can be stored in Session.

You would have to put this somewhere in your c# code.

Session["UserName"] = Address_Box.Text;

For a more plete answer I'd have to see more of your code. For a better solution you'd have to explain what you're trying to achieve

I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.

I can assign strings to these Session variables but i cannot assign values from Form elements to these variables

function SetUserName() 
{       
     <% var text1= "Charles";%>
     <%Session["UserName"] =text1;%>
     var session_value='<%=Session["UserName"]%>';
}
</script>

This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.

function LoadUserName()
{
    var username = '<%= Session["UserName"] %>';
    alert(username);
}

This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?

For Example Say i have a Text Box

 <asp:TextBox ID="Address_Box" runat="server"  TextMode="MultiLine"
             MaxLength="200" ></asp:TextBox>

How can I send the text from this box to the session variable?

I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.

I can assign strings to these Session variables but i cannot assign values from Form elements to these variables

function SetUserName() 
{       
     <% var text1= "Charles";%>
     <%Session["UserName"] =text1;%>
     var session_value='<%=Session["UserName"]%>';
}
</script>

This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.

function LoadUserName()
{
    var username = '<%= Session["UserName"] %>';
    alert(username);
}

This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?

For Example Say i have a Text Box

 <asp:TextBox ID="Address_Box" runat="server"  TextMode="MultiLine"
             MaxLength="200" ></asp:TextBox>

How can I send the text from this box to the session variable?

Share Improve this question edited Jun 5, 2014 at 9:35 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Jun 5, 2014 at 9:27 MarsOneMarsOne 2,1865 gold badges30 silver badges54 bronze badges 6
  • 1 javascript directly can not "directly" set backend session variable. – Sandeeproop Commented Jun 5, 2014 at 9:31
  • JavaScript does not have sessions. ASP does. – kraxor Commented Jun 5, 2014 at 9:31
  • Not knowing something doesn't get downvote - I corrected. He's here because he doesn't know – Ali Habibzadeh Commented Jun 5, 2014 at 9:34
  • You can use cookies or HTML5 local storage for storing form values. But notice that such data are stored on client's machine which can be wiped away ! – Rahul Gupta Commented Jun 5, 2014 at 9:35
  • Thnks Xgreen, But is it not possible to assign a HTML element value to these variable instead of "Charles". Till now it works fine... – MarsOne Commented Jun 5, 2014 at 9:35
 |  Show 1 more ment

2 Answers 2

Reset to default 1

Your code:

var session_value='<%=Session["UserName"]%>';

does not "access the ASP.Net session from javascript".

With this code you do generate (during the processing of the request) some text that will be sent to the browser. This text contains the string that results from the expression Session["UserName"].

Once this text arrives at the browser it is interpreted as javascript. This javascript has no knowledge of any session values or the fact that it was (partly) generated.

You need to find some way to send data from the browser back to the server (using postback or ajax, for instance) before it can be stored in Session.

You would have to put this somewhere in your c# code.

Session["UserName"] = Address_Box.Text;

For a more plete answer I'd have to see more of your code. For a better solution you'd have to explain what you're trying to achieve

本文标签: htmlHow to assign values from form to Javascript session variablesStack Overflow