在C#中定义会话变量从javascript获取 - javascript

我在后面的代码中将表示URL的字符串保存在会话变量中,如下所示:

        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"]

我还能如何访问会话变量,或者我做错了什么?

 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中访问该hiddenvariable:
假设hiddenvariable的ID为“ hdnxyz”

var togo = $('#hdnxyz')。val();

var newPage = togo;

Javascript-从当前网址中删除查询字符串 - javascript

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…

在两个值之间匹配并返回正则表达式 - javascript

我正在尝试使用正则表达式从字符串中获取值,该值是tt="和"&之间的文本的值因此,例如,"tt="Value"&"我只想从中得到单词"Value"。到目前为止,我已经有了:/tt=.*&/这给了我"tt=Value"&,然后,要…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

JavaScript中的字符串评估函数 - javascript

            JavaScript中是否有任何内置函数,例如Python中的eval内置函数?注意:eval函数将方程式作为字符串并返回结果。例如,假设变量x为2,则eval("2x+5")返回9。 参考方案 是的,JavaScript中也有eval函数。此外,该声明应有效用于评估,即eval("2*x+5"…

如何在JQuery中操作JSONArray - javascript

我有一个php函数,它以JSON返回此代码{"0":{"title":"Dans l\u2019appartement"},"1":{"title":"A l\u2019a\u00e9roport - D\u00e9part de B\u00e9at…