2012-03-15 25 views
2

在我的代碼隱藏文件中的事件內我想運行一個腳本,在3秒後將用戶重定向到特定頁面。我知道我可以使用下面的Page.ClientScript行和setTimeout,但是我需要幫助的是實際將setTimeout語句放到哪裏以使其工作?在事件中使用js代碼重定向

底部是我用於重定向的代碼行,我想用Page.ClientScript行代替。

在此先感謝!

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

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

注:我曾嘗試沒有任何的運氣如下:

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

回答

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

與要重定向到URL替換目標。

您可以從代碼隱藏動態地構建目標網址:

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

感謝。 Okey,但如何將動態數字,這是網址(movieID)的一部分? – holyredbeard 2012-03-15 15:30:50

+0

我試過了:Page.ClientScript.RegisterStartupScript(this.GetType(),「alert」,「setTimeout('top.location.href =」+ String.Format(「〜/ Edit.aspx?id = {0}」, movie.MovieID)+「',3000);」,true);但它似乎並沒有工作:/ – holyredbeard 2012-03-15 15:34:12

+0

@JasonCraig,我發佈了一些新的代碼。 – 2012-03-15 15:38:47