Question Set Session variable in JS-ASP.net

sonia.sardana

Active member
Joined
Jan 25, 2009
Messages
35
Programming Experience
Beginner
I know how to access Server Side Session variable at client side(JS)..But i want to know how to Set Sessin varable at Clinet side(JS) & access its value at Server side..If somebody knows help me out!!!
 
Set Session object using client-side code JavaScript

Hello Sonia,

To set Session object you need a bit AJAX :)
Just create ax extra asp.net page which accepts the parameter(s) you need to set and then just call the page with the necessary parameter(s)

For example (very basic example):

VB.NET:
[FONT=courier new]var query = "setsession.aspx?sessionvalue= " + some-client-side-value;[/FONT]
[FONT=courier new]  request = new ActiveXObject("Microsoft.XMLHTTP");[/FONT]
[FONT=courier new]request[/FONT][FONT=courier new].open("POST", [/FONT][FONT=courier new]query[/FONT][FONT=courier new], true);                [/FONT]
[FONT=courier new]request[/FONT][FONT=courier new].send();[/FONT]
Setsession.aspx.cs

VB.NET:
[FONT=courier new]private void Page_Load(object sender, System.EventArgs e)[/FONT]
[FONT=courier new]{[/FONT]
[FONT=courier new]    if(Request.QueryString["[/FONT][FONT=courier new]sessionvalue[/FONT][FONT=courier new]"] != null)[/FONT]
[FONT=courier new]    {[/FONT]
[FONT=courier new]         Session["[/FONT][FONT=courier new]mysession[/FONT][FONT=courier new]"] =Request.QueryString["[/FONT][FONT=courier new]sessionvalue[/FONT][FONT=courier new]"].ToString();[/FONT]
[FONT=courier new]    }[/FONT]
[FONT=courier new]}[/FONT]
 
nb. the previous post isn't cross browser compatible - you might want to use an AJAX framework
 
This is a VB.NET version of previously published sample code:

VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Request.QueryString("sessionvalue") isNot Nothing Then 
            Session("mysession") = Request.QueryString("sessionvalue").ToString()
      End If
End Sub
 
Last edited:
I think you meant

VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Request.QueryString("sessionvalue")[B] Is Not Nothing[/B] then 
            Session("mysession") = Request.QueryString("sessionvalue").ToString()
      End If
End Sub
 
Back
Top