[英]how to pass session value from code behind to javascript
i have made a session variable Session["Background1"] = value; 我做了一个会话变量Session [“ Background1”] = value; in one of my code behind function i want to retrieve this value in my javascript function.
在我的函数背后的代码之一中,我想在我的javascript函数中检索此值。
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "SessionValue", "var sessionValue = '" + Session["Background1"] + "';", true);
Personally, I prefer to do it the scripting way. 就个人而言,我更喜欢用脚本编写方式。 Suppose your variable is currently declared in your Javascript as:
假设您的变量当前在Javascript中声明为:
var background1 = null; // TODO: Add value from session.
To add the value from session, all you need to do is this: 要从会话中添加值,您需要做的是:
var background1 = '<%= Session["Background1"] %>';
When the page is output by ASP.NET, the expression between <%=
and %>
is evaluated and written to the page, effectively becoming a Response.Write
. 当ASP.NET输出页面时,将评估
<%=
和%>
之间的表达式并将其写入页面,从而有效地成为Response.Write
。 As long as the member is available in your page at the public or protected level, you can push it into your Javascript it in this way. 只要该成员可以在您的页面上以公共或受保护的级别使用,您就可以通过这种方式将其推送到Javascript中。
I find this approach easier to work with than the obnoxiously verbose ClientScriptManager
. 我发现这种方法比讨厌的冗长的
ClientScriptManager
更易于使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.