Is it possible to delay the response.redirect in an asp.net page by 5 seconds?
something like delay.response.redirect("myURL.aspx")
I need to run a jquery animation before the page redirects.
I don't want to use meta refresh if I can help it but if that's the only way or the best way then please let me know.
thanks
if you posted to the server, there's no way to affect client anymore - all server can do is wait. And sleeping in web server is very bad - exhausts thread pool, bad for performance.
What you should do is delay on the client. Alternatively, you can post to that page using AJAX - that's the one I would prefer. Post to server using ajax, then get the response and wait 5 seconds (in javascript) before changing location.href
You can even read the new location from the server (from the data returned by ajax call)
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('Cliente cadastrado com sucesso.'); setTimeout(function(){window.location.href ='../../Default.aspx'}, 3000);", true);
Just before doing a Response.Redirect, add the line for System.Threading.Thread.Sleep(5000);
Response.AddHeader("REFRESH", "5;URL=http://redirectPage.aspx")
5 = delay in seconds.
I created a Hyperlink on the client, and control the NavigateURL of this from the server. I then wait until page load on the client, extract the url from this and use javascript to initiate the redirect after a period of time (you could wait until your animation has finished):
Client
<asp:Content ID="Head" ContentPlaceHolderID="ContentHead" runat="server">
<script type="text/javascript">
$(document).ready(function () {
redirectUrl = $('#mainbody').find('a').attr('href');
setTimeout(function(){
window.location.replace(redirectUrl);
}, 5000);
});
</script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="mainbody">
<asp:HyperLink ID="redirectUrl" runat="server" Visible="false" />
</div>
</asp:Content>
Server
redirectUrl.NavigateUrl = "ValentinesDay.aspx";
You could hard code the redirect URL in javascript but I need to be able to control this from the server.
Is it possible to delay the response.redirect in an asp.net page by 5
seconds?
something like delay.response.redirect("myURL.aspx")
<%
response.write("<body onload=""myFunction()""><script>function myFunction()_
{setTimeout(function newDoc()_
{window.location.assign(""http://www.stackoverflow.com"")},5000);}_
</script>")
%>
For a non-client-side solution, i.e. one that relies on the browser to implement the delay, you could create a proxy page which contains a server-side delay and then redirects to the final destination page.
myPostPage.aspx
processes the data then calls response.redirect("myProxyPage.aspx?d=myDestinationPage.aspx", false)
Then myProxyPage.aspx
can have a timer, System.threading.Thread.Sleep("5000")
, then redirect to the target page: response.redirect(request.querystring("d"), false)
This should give your first page enough time to complete before the final page is called.