Javascript Alert before redirecting in ASP.NET

Rohit Chaudhari picture Rohit Chaudhari · Aug 31, 2012 · Viewed 30.7k times · Source

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");

Answer

Vishal Suthar picture Vishal Suthar · Aug 31, 2012

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);