2

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.

2
  • session timer is reset when you interact with the server and the session - so page load will reset time out (to 15 mins as per your info), as will any other requests. When session has expired a new session is created when a new request comes in for a page (new session token) (don't get confused with authentication) - you can compare session tokens to see if you have a new session - when session expired all data in it will be removed from server. You need another "management" api to query separately that would return this info - else it a bit like Schrödinger's cat Commented Apr 8, 2020 at 9:43
  • If your ultimate goal is to have live information of session state - you cant send request to the session you are monitoring - it will reset the timer, and thus you have then created a keep-alive request. you need to create a separate app - that has access to the session data, but is itself accessible via another session - you can pass the session token to be monitored to this other API, and it can then look up the session to see its state. Why do you want this?? Commented Apr 8, 2020 at 9:47

1 Answer 1

2

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"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.