11

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

3 Answers 3

28

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);
1
  • This solution also works when using routing names instead of full page names.
    – Enrico
    Jul 25, 2017 at 10:09
3

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";
1
  • How could I add this to the builtin function? Aug 31, 2012 at 6:32
0

This Works fine

                string message = "Upadate Successfull !!";
                string url = "/Post.aspx";
                string script = "{ alert('";
                script += message;
                script += "');";
                script += "window.location = '";
                script += url;
                script += "'; }";
                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "alert", script, true);
                
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.