如何从表单中赋值给Javascript会话变量 [英] How to assign values from form to Javascript session variables

查看:109
本文介绍了如何从表单中赋值给Javascript会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建Javascript会话变量,它将从一个表单读取信息并将这些变量传递给另一个表单。



我可以将字符串赋给这些Session变量,但是我无法将Form元素的值赋给这些变量

$ $ p $函数SetUserName()
{
<%var text1 =Charles;%>
<%Session [UserName] = text1;%>
var session_value ='<%= Session [UserName]%>';
}
< / script>

这是Javascript的第一页,它的作品和一个按钮点击我打电话给另一个页面,我可以显示这个会话变量。

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

这会在第二页加载时被调用。如何将HTML元素的值传递给会话变量?



例如说我有一个文本框

 < asp:TextBox ID =Address_Boxrunat =serverTextMode =MultiLine
MaxLength =200>< / asp:TextBox>

如何将文本从此框发送到会话变量?

$ b $ ;%=会话[ 用户名]%GT;';

不会从JavaScript访问ASP.Net会话。

使用此代码,您可以生成(在处理请求期间)一些将发送到浏览器的文本。此文本包含从表达式 Session [UserName]



的字符串。一旦此文本到达该浏览器被解释为JavaScript。这个JavaScript没有任何会话值的知识或它(部分)生成的事实。



您需要找到一些方法将数据从浏览器发回到服务器(例如使用回发或ajax),然后才能存储在Session中。


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?

解决方案

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.

这篇关于如何从表单中赋值给Javascript会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆