C# 如何在jquery方法中获取asp.net Session值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/856553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get asp.net Session value in jquery method?
提问by Ravi
I want to access a Session value in jquery method in the ASP.NET MVC view page. See the below code,
我想在 ASP.NET MVC 视图页面中的 jquery 方法中访问一个会话值。看下面的代码,
$('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', '<%= Session["CoBrowse"].ToString() %>');
How can I get the Session value in jquery.
如何在 jquery 中获取 Session 值。
采纳答案by Ravi
$('input,select').attr('disabled','<%=Session["CoBrowse"].ToString() %>');
回答by Fermin
Not sure if this is the best route but within your aspx page you could create a method that returns the value of your session variable, e.g.
不确定这是否是最佳路线,但在您的 aspx 页面中,您可以创建一个返回会话变量值的方法,例如
Server side:
服务器端:
using System.Web.Services;
[WebMethod(EnableSession = true)]
public static string GetSession()
{
return Session["CoBrowse"].ToString();
}
then call this method client side using jQuery:
然后使用 jQuery 调用此方法客户端:
$.ajax({
type: "POST",
url: "./Default.aspx/GetSession",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result){
('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', result.d);
}
});
回答by Kobi
Many comments:
许多评论:
- You cannot "Access Session from jQuery". You use MVC and asp.net to create an HTML page (with JavaScript). Session is a server-side object, and JavaScript runs on the client side.
- Take a look at jQuery's selectors. They have useful selectors like
:checkbox
,:text
, etc. - Your code produce the JavaScript you expect: it compiles, runs, and produces JavaScript with
true
orfalse
on the right place. - This is not the way to disable an element. If an element has the 'disabled' attribute it will be disabled, no matter the value.
<input type="checkbox" disabled="false" />
is also a disabled check box, so your controls are always disabled. If that is the way you choose anyway, consider:
var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>; if(!isCoBrowse) //disable controls $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.
This will produce the client-side JavaScript code:
var isCoBrowse = true;
And, to enable an element:
$("input").removeAttr("disabled");
- 您不能“从 jQuery 访问会话”。您使用 MVC 和 asp.net 创建一个 HTML 页面(使用 JavaScript)。Session 是一个服务器端对象,JavaScript 运行在客户端。
- 看看jQuery 的选择器。它们有像有用的选择
:checkbox
,:text
等等。 - 您的代码生成您期望的 JavaScript:它在正确的位置
true
或false
在正确的位置编译、运行和生成 JavaScript 。 - 这不是禁用元素的方法。如果元素具有 'disabled' 属性,它将被禁用,无论值如何。
<input type="checkbox" disabled="false" />
也是一个禁用复选框,因此您的控件始终处于禁用状态。 如果这是您选择的方式,请考虑:
var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>; if(!isCoBrowse) //disable controls $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.
这将生成客户端 JavaScript 代码:
var isCoBrowse = true;
并且,要启用一个元素:
$("input").removeAttr("disabled");
Also, there are much better ways to accomplish this. Have you considered disabling the controls on server side, if possible?
此外,还有更好的方法来实现这一点。如果可能,您是否考虑过禁用服务器端的控件?
回答by Cody C
If that Session variable is sensitive data (which in this case it probably isn't), I would not use this solution as it would show the Session data when you looked at the javascript source. If it is sensitive session data, accessing it via a web method (see above answer) is probably best.
如果该会话变量是敏感数据(在这种情况下可能不是),我不会使用此解决方案,因为它会在您查看 javascript 源时显示会话数据。如果它是敏感的会话数据,通过网络方法访问它(见上面的答案)可能是最好的。
回答by fyalavuz
<input id="sessionInput" type="hidden" value='<%= Session["name"] %>' />
var getSessionValue = $('#sessionInput').val();
回答by user1633617
Easy! when you know how:
简单!当你知道如何:
@Html.Encode(Session("classificationTitle"))...and in the .js file:
...在 .js 文件中:
var classificationTitle = document.getElementById('classificationTitle').innerHTML;
sorry - I can't post the full html as this site strips out angle brackets :(
抱歉 - 我不能发布完整的 html,因为这个网站去掉了尖括号 :(