我在后面的代码中将表示 URL 的字符串保存在 session 变量中,如下所示:
String mois = Request.QueryString["mois"].ToString();
String m = mois;
String moisnom = Request.QueryString["moisnom"].ToString();
String annee = Request.QueryString["annee"].ToString();
String dt = Request.QueryString["date"].ToString();
String user = Request.QueryString["user"].ToString();
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
Session["togo"] = where;
然后我尝试在 JavaScript 中得到它,如下所示:
var togo = '<%=Session["togo"]%>';
// i also tried this var togo ='@Session["togo"]';
var newPage = togo; // this should contain a string with the url to go to
但是当我使用它时,它会将其用作字符串,这是我的 URL 的样子:
http://localhost:50311/<%=Session["togo"]%>
or
http://localhost:50311/@Session["togo"]
我还能如何访问 session 变量或者我做错了什么?
EDIT:
like you suggested i already tried using the hidden field like this
yes i tried that but then i had this problem here is the definition of the hidden field
<input type="hidden" value="aa" id="myHiddenVar" runat="server"/>
然后我尝试为其提供点击时所需的值
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
myHiddenVar.Value= where;
这就是我尝试从 js 文件获取它的方法
var togo = $('#myHiddenVar').val();
var newPage = togo;
但它采用默认值,意思是“aa”,如 value="aa"我猜是因为脚本在变量赋值之前执行,如何反转该顺序?
最佳答案
session 结束后["togo"] = 地点; 将此 session [“togo”]保存在隐藏变量中
hiddenVariable= session ["togo"];
现在在 JS 中访问隐藏变量: 假设隐藏变量的ID是“hdnxyz”
var togo = $('#hdnxyz').val();
var newPage = 多哥;
关于javascript - 在 C# 中定义 session 变量从 javascript 获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287830/