vb.net 将会话变量传递给另一个页面 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16315812/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:26:27  来源:igfitidea点击:

Pass Session Variable to another page Javascript

javascriptasp.netvb.netsession-variables

提问by user2217039

I guys, I'm creating a VB.NET application, and this is the first time that I used Session Variables.

伙计们,我正在创建一个 VB.NET 应用程序,这是我第一次使用会话变量。

What I want to do is to pass the session variable from a page (first.aspx) to another page (second.aspx) via url.

我想要做的是通过 url将会话变量从一个页面(first.aspx)传递到另一个页面(second.aspx)。

Here an example of what I want to do:

这是我想要做的一个例子:

In the first.aspx.vbpage I declare a session variable and assign to it a value

first.aspx.vb页面中,我声明了一个会话变量并为其分配了一个值

Session("example") = "example123"

Then I pass this Session Variable to the first.aspxpage

然后我将这个会话变量传递给first.aspx页面

Response.Write(Session("example"))

and via javascript read the value of the Variable

并通过javascript读取变量的值

<script type= text/javascript>
    var SessionVar = '<%=Session("example")%>)';
</script>

Now, what I want to do is to change the value of the Variable (for example setting it as example456), and then pass the variable to the second.aspx[for example with window.open()] so that the url not contains the value of the variable but its name:

现在,我想要做的是更改变量的值(例如将其设置为example456),然后将变量传递给second.aspx[例如使用window.open()] 以便 url 不包含变量的值而是它的名字:

url/second.aspx?value=exampleAND NOTurl/second.aspx?value=example456

url/second.aspx?value=example并不是url/second.aspx?value=example456

Infact I don't want that the user read the value of the variable.

事实上,我不希望用户读取变量的值。

Finally I have to read the value of the session variable in the second.aspx.vbvia Request.QueryString("value")and do the various operations.

最后,我要读的会话变量的值second.aspx.vb通过Request.QueryString("value")并执行各种操作。

Is it possible ?

是否可以 ?

If not, there is anothe way to do this

如果没有,还有另一种方法可以做到这一点

Thanks for the help :)

谢谢您的帮助 :)

回答by The Coding Monk

Session variables are called this way because they are passed automatically from one page to the other within the same session, so you should just modify the value of the variable whenever you want and then access it again in the second page to find the value. You don't need to pass anything explicitely.

会话变量以这种方式调用是因为它们在同一会话中自动从一个页面传递到另一个页面,因此您应该随时修改变量的值,然后在第二个页面中再次访问它以找到该值。你不需要明确地传递任何东西。

回答by ajakblackgoat

To set session variable from javascript can be done like below:

要从 javascript 设置会话变量,可以按如下方式完成:

  1. Create a hidden field control on the first page

    <asp:HiddenField ID="HiddenField1" runat="server" />
    
  2. Client script to set value of the hidden field

    function setVal(value) {
        // if using jQuery
        $("#<%= HiddenField1.ClientID%>").val(value);
    
        // if using javascript
        var hf = document.getElementById("<%= HiddenField1.ClientID%>");
        hf.value = value;
    }
    
  3. Create a button or link to navigate to the second page

    <asp:Button ID="Button1" runat="server" Text="Go to second page" OnClick="NavigateToSecondPage" /> 
          or 
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="NavigateToSecondPage">Go to second page</asp:LinkButton>
    
  4. In the first page code behind, create the NavigateToSecondPagesub to handle the onclick event of the button/link

    Protected Sub NavigateToSecondPage(sender As Object, e As EventArgs)
        Session("example") = HiddenField1.Value
        Response.Redirect("SecondPage.aspx")
    End Sub
    
  1. 在第一页创建隐藏字段控件

    <asp:HiddenField ID="HiddenField1" runat="server" />
    
  2. 客户端脚本设置隐藏字段的值

    function setVal(value) {
        // if using jQuery
        $("#<%= HiddenField1.ClientID%>").val(value);
    
        // if using javascript
        var hf = document.getElementById("<%= HiddenField1.ClientID%>");
        hf.value = value;
    }
    
  3. 创建按钮或链接以导航到第二页

    <asp:Button ID="Button1" runat="server" Text="Go to second page" OnClick="NavigateToSecondPage" /> 
          or 
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="NavigateToSecondPage">Go to second page</asp:LinkButton>
    
  4. 在第一页代码后面,创建NavigateToSecondPagesub来处理按钮/链接的onclick事件

    Protected Sub NavigateToSecondPage(sender As Object, e As EventArgs)
        Session("example") = HiddenField1.Value
        Response.Redirect("SecondPage.aspx")
    End Sub
    

Therefore, in your second page, you can access the Session("example") value and it will have a new value set by the client script in the first page.

因此,在您的第二页中,您可以访问 Session("example") 值,它将在第一页中由客户端脚本设置一个新值。