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
2 Answers
Reset to default 1Your 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
2 Answers
Reset to default 1Your 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
版权声明:本文标题:html - How to assign values from form to Javascript session variables - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745530733a2154737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论