title: Show alert message before redirecting to other page ASP.NET
body: <p>I have found a lot of examples but I don't know why these are not working for me. I have tried these but in vain.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/33560508/how-to-show-javascript-alert-message-before-redirecting-to-same-page-in-vb-net">How to show Javascript Alert message before redirecting to same page
  in VB.NET? </a>   </li>
  <li><a href="https://stackoverflow.com/questions/12209822/javascript-alert-before-redirecting-in-asp-net">Javascript Alert before redirecting in ASP.NET </a>  </li>
  <li><a href="https://stackoverflow.com/questions/782354/asp-net-webform-display-alert-and-redirect">Asp.net Webform Display Alert and redirect </a></li>
  </ul>
  
  <p>I have also tried the following:</p>
  
  <pre><code>String message = "Session Expired! Log In Again.";
  ClientScript.RegisterStartupScript(
      this.GetType(),
      Guid.NewGuid().ToString(), 
      string.Format("alert('{0}');window.location.href = 'default.aspx'", 
      message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")), 
      true);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>ScriptManager.RegisterStartupScript(
      this, 
      this.GetType(), 
      "redirect", 
      "alert('Session Expired! Log In Again.'); 
      window.location='" + Request.ApplicationPath + "Account/login.aspx';", 
      true);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Response.Write("
  &lt;script language='javascript'&gt;
      window.alert('Session Expired');
      window.location='~/Account/Login.aspx';
  &lt;/script&gt;");
  </code></pre>
  
  <p>but all of these are not redirecting to the login page just showing the alert message.</p>