如何使用javascript或jquary检查asp.net c#中的会话变量? [英] how to check session variable in asp.net c# using javascript or jquary?

查看:55
本文介绍了如何使用javascript或jquary检查asp.net c#中的会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net c#page中使用java脚本或jquary获取会话变量。

How to get session variable using java script or jquary in asp.net c# page.

推荐答案

使用以下方法检查/获取会话值。



Use below method for check/get session value.

.aspx code


.ajax({
type:POST,
url:PageURL / CheckSession,
data:JSON.stringify({'SessionName':'UserId'}),
contentType:application / json; charset = utf-8,
数据类型:json,
成功:函数(结果){
result = result.d;
alert(result);
},
错误: function(result){
alert(result.d);
}
});

c #code
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string CheckSession(string SessionName)
{
if(HttpContext.Current.Session [SessionName]!= null)
{
return Convert.ToString(HttpContext.Current.Session [SessionName]);
}
else {
返回失败;
}
}
.ajax({ type: "POST", url: "PageURL/CheckSession", data: JSON.stringify({ 'SessionName': 'UserId' }), contentType: "application/json; charset=utf-8", datatype: "json", success: function (result) { result = result.d; alert(result); }, error: function (result) { alert(result.d); } }); c# code [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string CheckSession(string SessionName) { if (HttpContext.Current.Session[SessionName] != null) { return Convert.ToString(HttpContext.Current.Session[SessionName]); } else { return "fail"; } }






你也可以分配使用asp.net脚本块对JavaScript变量进行会话值。



...就像这个例子:

Hi,

You can also assign session values to JavaScript variables by using asp.net script blocks.

... like this example:
<script type="text/javascript" language="javascript">
var sUserName = '<%= Session["UserName"] %>';
alert(sUserName);
</script>





...希望它有所帮助。



... hope it helps.


这篇关于如何使用javascript或jquary检查asp.net c#中的会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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