asp.net中的javascript中的会话 [英] session in javascript in asp.net

查看:77
本文介绍了asp.net中的javascript中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过javascript函数为会话分配一个值

i want to assign a value to a session from javascript function

<%Session["Test"] = "Welcome Mamu";%> 




上面的代码将正常工作.但是我想从像
这样的变量中赋值




the above code will work properly. but i want to assign a value from a variable like

var strTest=document.getElementById(''DropDownList1'').value;
<%Session["Test"] = "''+ strTest +''";%>




这很不错,但是会话中保存的值是"+ strTest +",而不是strTest
的值
是否有人知道解决方案请尽快回复我




this is woking but the value saved in the session is ''+ strTest +'' not the value of strTest

is anybody know the solution plz reply me soon

推荐答案

当然,存储在种子中的值是"+ strTest +",因为这是您分配给Session的值[测试"].
由于<%%>内部的代码是在页面呈现期间在服务器端执行的,var strTest = ...位是javascript,是在客户端(浏览器)上运行的代码.如果要设置会话变量,则必须提交数据(包括DropDownList1的数据),或者如果DropDownList1的值发生更改,则必须对您的网站进行AJAX调用.

最好的问候,

-MRB
Of course the valuue stored in the seesion is ''+ strTest +'' because that is what you assigned to Session["Test"].
What you''re trying to achieve will not work this way since the code inside the <% %> is executed on the serverside during rendering of the page and the var strTest = ... bits are javascript are code that is run on the client side (browser). If you want to set the session variable you''ll either have to submit the data including that of DropDownList1 or do an AJAX call to your website if the value of the DropDownList1 changes.

Best Regards,

-MRB


我认为您误解了他的问题.他想通过JavaScript访问Session变量,这是不可能的.这纯粹是由于该会话已加密,主要是为了防止黑客入侵.试想一下,如果可以通过JavaScript进行访问,那么眨眼间就会有多少个网站被黑客入侵:)

但是根据您的需求,您也许可以解决它.我想到的一个简单方法是,使用AJAX调用服务器端方法以从会话中获取所需的值.但是请记住,不建议这样做.您可以调用的方法,其他任何人都可以调用:)除非您对访问进行身份验证.

我的建议是使用页面中的隐藏字段来携带要使用javascript处理的值.

希望这会有所帮助.

谢谢,帕桑.
I think you misunderstood his question. He wants to access the Session variable through JavaScript which is impossible. This is purely due to that session is encrypted, mainly to guard it off from hackers. Just think, if it was accessible from JavaScript, how many websites will be hacked in a blink :)

But depending on what you want, you might be able to workaround it. A simple method I would think of is, use AJAX to call a server-side method to fetch the value you want from the session. But please, keep it in mind that this is NOT RECOMMENDED. A method that you can call, anyone else can call :) Unless, you authenticate the access.

My advice to you is to use a hidden field in your page to carry the values that you want to process using javascript.

Hope this helps.

Regards, Pasan.


您从根本上误解了会话是什么.会话存储在服务器上,用于在同一用户的页面请求之间保留数据.在客户端(即在浏览器中)不可用,因为浏览器显示的是一页.

设置会话变量来响应客户端操作是没有意义的,因为客户端操作在一页的上下文中发生,并且不需要在请求之间持久化.

如果要记录用户的选择,则应通过AJAX或表单提交(取决于情况;对于单个值使用AJAX,一次完整地提交表单)来提交该选择.响应该提交的服务器页面可能选择在会话中存储一些信息,但可能不会.
You fundamentally misunderstand what the session is. The session is stored on the server and is used to persist data between page requests from the same user. It is not available on the client side (i.e. in the browser) because the browser is showing one page.

It does not make sense to set session variables in response to client actions, as client actions are occuring within the context of one page and do not need to be persisted between requests.

If you want to take a record of a user''s choice, you should submit that choice, either by AJAX or a form submission (depending on the circumstances; AJAX for single values, form submissions for a whole set at once). The server page that responds to that submission might choose to store some information in the session, but it might not.


这篇关于asp.net中的javascript中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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