Javascript Alert before redirecting in ASP.NET

31,700

Solution 1

Display the alert with javascript and then do the redirect with the same:

ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('Time OutAlert'); window.location='" + 
Request.ApplicationPath + "Nextpage.aspx';",true);

Solution 2

You can not do that, the way you try because the message is running on the client side, but you make the redirect on code behind before the page loading to show the message.

The way to do that is to call right after the message a client side redirect as:

window.location = "NextPage.asps";
Share:
31,700
Rohit Chaudhari
Author by

Rohit Chaudhari

Hi, I am Computer Engineer, and Working as a Lead Engineer, Business Intelligence.

Updated on February 11, 2020

Comments

  • Rohit Chaudhari
    Rohit Chaudhari over 4 years

    I am using following code to display message while updating in update panel

    string jv = "alert('Time OutAlert');";
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", jv, true);
    

    It works fine.

    But when I use Redirect after it it loads the page without displaying the message. I want user to see the message and after clicking on "ok" it should redirect.

    string jv = "alert('Time OutAlert');";
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", jv, true);
    Response.Redirect("~/Nextpage.aspx");
    
  • Rohit Chaudhari
    Rohit Chaudhari almost 12 years
    How could I add this to the builtin function?
  • Enrico
    Enrico almost 7 years
    This solution also works when using routing names instead of full page names.