Make a simple JavaScript Alert pop-up in C# code behind after a session ends

177 Views Asked by At

What I am trying to do is that I have used a function in the Global.asax.cs file to detect when a user's login session has expired and if it is expired then the user should be redirected to the login page, code shown below:

protected void Session_Start(object sender, EventArgs e)
    {
        if (Context.Session != null)
        {
            if (Session.IsNewSession)
            {
                HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"];
                if (newSessionIdCookie != null)
                {
                    string newSessionIdCookieValue = newSessionIdCookie.Value;
                    if (newSessionIdCookieValue != string.Empty)
                    {
                       //I want to let the client side page to pop-up an alert to indicate the user 
                       //that the session is expired.

                        Response.Write("<script>alert('Your Session is out, please re-login!');</script>");

                        // This means Session was timed Out and New Session was started
                        Response.Redirect("Login.aspx");
                    }
                }
            }
        }
    }

However, the issue for my current code is that when the session expired, the page will be redirected directly, there is no pop up of alert at all.

Could someone help? Thanks in advance!

0

There are 0 best solutions below