Reputation: 25
I am using asp.net, vb.net, and jquery. I need to see if my session exists or not in jQuery. I know that I cannot work with sessions in jQuery due to JavaScript being client side while sessions are server side. So, I thought a work-around could be storing the session info in a label.
Here is what I am trying that is not quite working:
So on Page_Load, I set the textbox to session (which equals "True" when the session exists): LblSession.Text = Session("MySession")
Also on Page_Load, if the session expires (set to 15 mins), then I want the label to update the session to be null / false.
If Not Session("MySession") Then LblSession.Text = Session("MySession")
However when my session ends, this label doesn't update to if the session exists or not.
On my jQuery, I get the value of the session (if it exists, it is True, otherwise, it is false or null): var mySession = $("#LblSession").text();
//Check to see if the session is true
if (mySession == "True") {
'do something if session exists'
} else {
'do something if session does not exist'
}
This is my ultimate goal, to be able to see if the session exists or if it expired from my jQuery.
Upvotes: 2
Views: 243
Reputation: 162
Your Javascript call to the ID html tag maybe is wrong, check that ID control exists:
<asp:Label runat="server" ID="LblSession"></asp:Label>
// render as:
<span id="[if inside Masterpage ex. ContentMain_]LblSession"></span>
$("#LblSession").text();
Check the configuration of web.config, value of tag clientIDMode, the ID depends of this settings and of the page structure, example if you have masterpages and container controls. Check the html generated code, to get the client ID tag of the rendered html control.
<pages clientIDMode="Predictable"></pages>
More info: (https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.clientidmode?view=netframework-4.8)
Better use a hidden field, render as hidden input: (https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.hiddenfield?view=netframework-4.8)
<asp:HiddenField id="HiddenField1" runat="server" value="1"/>
Upvotes: 2