holyredbeard
holyredbeard

Reputation: 21208

Redirecting with js code in event

Inside an event in my code behind file I want to run a script that redirect the user to a specific page after 3 seconds. I understand that I can use the Page.ClientScript line below with a setTimeout, but what I need help with is what to actually put inside the setTimeout statement to get this works?

At the bottom is the code line I use for redirecting, that I want to replace with the Page.ClientScript line.

Thanks in advance!

Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", "setTimeout('???', 3000);", true);

Response.Redirect(String.Format("~/Edit.aspx?id={0}", movie.MovieID), false);

NOTE: I have tried the following without any luck:

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "setTimeout('top.location.href = " + String.Format("~/Edit.aspx?id={0}", movie.MovieID) + "', 3000);", true);

Upvotes: 2

Views: 275

Answers (1)

Saeb Amini
Saeb Amini

Reputation: 24400

setTimeout("top.location.href = 'TARGET'", 3000);

Replace TARGET with the URL you want to redirect to.

You can build the target URL dynamically from the codebehind:

string targetUrl = String.Format("/Edit.aspx?id={0}", movie.MovieID);
string javaScript = "setTimeout(\"top.location.href = '" + targetUrl + "'\", 3000);";

ClientScript.RegisterStartupScript(typeof(Page), "redirect", javaScript, true);

Upvotes: 2

Related Questions