开发者

set Timeout before redirrect page in C#

How to implement timeout in this code:

Res开发者_开发技巧ponse.Write(@"<script language='javascript'>alert('some alert');</script>"); 
Response.Redirect(Request.ApplicationPath);

I want to show to user message, and after redirect. But in my solution operations occurs very fast, and alert is not shown.

thanks


Remove the Response.Redirect and just do

Response.Write(@"
<script language='javascript'>
 alert('some alert');
  self.location = "/mypage.aspx";
</script>");

Any response.redirect in code will ignore any content already sent to the browser


You can use a META Refresh tag. This will do the redirect even if Javascript is disabled. The 10 below is the timeout in seconds before the redirect is done:

<meta http-equiv="refresh" content="10;url=<%=Request.ApplicationPath %>">
<script>alert('some alert');</script>

Update:

In Firefox and Chrome at least, the timeout starts from when the user clicks OK. It makes more send to put your text on the page, rather than in an alert box, e.g.:

<meta http-equiv="refresh" content="10;url=<%=Request.ApplicationPath %>">
<p class="alert">some alert</p>


With Response.Redirect you send an http 302 code to the browser, "forcing" it to request another page. That's why nothing happens with your alert code.

If you want to notify the user about the transferring you can print a page with your message and a meta refresh interval. Or you can handle it by javascript:

1) Print your alert

2) Change window.location to your new path

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜