loviji
loviji

Reputation: 13080

set Timeout before redirrect page in C#

How to implement timeout in this code:

Response.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

Upvotes: 1

Views: 514

Answers (3)

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171411

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>

Upvotes: 1

mamoo
mamoo

Reputation: 8166

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

Upvotes: 0

Rippo
Rippo

Reputation: 22424

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

Upvotes: 4

Related Questions