asp.net - Pass Session Variable to another page Javascript - Stack Overflow

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

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

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

Here an example of what I want to do:

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

Session("example") = "example123"

Then I pass this Session Variable to the first.aspx page

Response.Write(Session("example"))

and via javascript read the value of the Variable

<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:

url/second.aspx?value=example AND NOT 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.vb via Request.QueryString("value") and do the various operations.

Is it possible ?

If not, there is anothe way to do this

Thanks for the help :)

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

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

Here an example of what I want to do:

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

Session("example") = "example123"

Then I pass this Session Variable to the first.aspx page

Response.Write(Session("example"))

and via javascript read the value of the Variable

<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:

url/second.aspx?value=example AND NOT 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.vb via Request.QueryString("value") and do the various operations.

Is it possible ?

If not, there is anothe way to do this

Thanks for the help :)

Share Improve this question asked May 1, 2013 at 9:36 user2217039user2217039 1312 gold badges4 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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

  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 NavigateToSecondPage sub 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
    

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 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335198a4623069.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信