Hi

I have found the following code for Redirecting the user to loginpage on Session timeout :

///////////
Protected WithEvents body As System.Web.UI.HtmlControls.HtmlGenericControl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 5000 & ");")

Response.Write("Wait for session to timeout..")

End Sub

////////

I am trying to use the same code in C#, but I get errors in the line:

body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 5000 & ");")

The errors I get are as below and all are in the same above line:

) expected
; expected
; expected
Invalid Expression Term')'
; expected

Please somebody let me know how to rectify the error.

Thanks
Sandy

Dani AI

Generated

The VB approach shown by (injecting a client-side timeout on page load) is fine in concept, but the C# errors come from language differences: C# uses + for string concatenation, string literals must escape internal double quotes, and each statement needs a trailing semicolon. tried a direct translation using VB-style concatenation, and correctly pointed out the escaping issue — a cleaner, safer pattern is to register the JavaScript from code-behind instead of building an onLoad attribute string manually.

A recommended server-side injection (keeps script tags correct and avoids mangling onload handlers):

int timeoutMs = (Session.Timeout * 60 * 1000) + 5000;
string js = "setTimeout(function(){ window.location.href = 'login.aspx'; }, " + timeoutMs + ");";
Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionRedirect", js, true);

Registering the script this way uses the page script manager and avoids duplicate/incorrect attribute syntax. See the ClientScriptManager/RegisterStartupScript documentation for details. (learn.microsoft.com)

Important caveats and alternatives: session timeout is a server-side concept — the server considers a session expired when no requests arrive within the timeout window, and Session_End only fires for InProc mode (it cannot notify the browser). Relying solely on a one-shot client timer can be inaccurate if other tabs, AJAX calls, or sliding expiration renew the session; a robust solution uses either a periodic AJAX “heartbeat” that asks the server whether the session is still valid (and redirects if not), or server-side checks on each request to issue a redirect when session state is missing. See the ASP.NET session-state and session-event docs and the forms-authentication sliding-expiration notes for background. (learn.microsoft.com)

Recommended Answers

All 2 Replies

Hi

I have found the following code for Redirecting the user to loginpage on Session timeout :

///////////
Protected WithEvents body As System.Web.UI.HtmlControls.HtmlGenericControl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 5000 & ");")

Response.Write("Wait for session to timeout..")

End Sub

////////

I am trying to use the same code in C#, but I get errors in the line:

body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 5000 & ");")

The errors I get are as below and all are in the same above line:

) expected
; expected
; expected
Invalid Expression Term')'
; expected

Please somebody let me know how to rectify the error.

Thanks
Sandy

Hi Sandy,
try the following code.

string str = "window.setTimeout(window.location.href='login.aspx'," & "(Session.Timeout * 60 * 1000) + 5000);";
body.Attributes.Add("onLoad",str )

Thanks,
Kedar

In c# you can't use "", you must use \". Also you forgot to add a ; at the end of the line. The correct code is:

body.Attributes.Add("onLoad", "window.setTimeout(\"window.location.href='login.aspx'\"," & (Session.Timeout * 60 * 1000) + 5000 & ");");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.