asp.net delay before response redirect
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 ref开发者_运维问答resh if I can help it but if that's the only way or the best way then please let me know.
thanks
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('Cliente cadastrado com sucesso.'); setTimeout(function(){window.location.href ='../../Default.aspx'}, 3000);", true);
Response.AddHeader("REFRESH", "5;URL=http://redirectPage.aspx")
5 = delay in seconds.
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)
Just before doing a Response.Redirect, add the line for System.Threading.Thread.Sleep(5000);
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.
Well, old question, but web forms are still in use. This works and allows you to still control the redirect from the server side, so it is easy to add dynamic parameters to the destination url.
On the .aspx page:
script
function timedRedirect() {
setTimeout(redirect, 3000)
}
function redirect() {
document.getElementById("btnRedirect").click();
}
/script
//Hidden button:
<asp:Button runat="server" ID="btnRedirect" Text="" style="display:none;" OnClick="BtnRedirect_Click" />
In codebend. Fire in relevant event:
Page.ClientScript.RegisterStartupScript(
GetType(),
"Delay",
"timedRedirect();",
true);
And your onclick event for the hidden button:
protected void BtnRedirect_Click(object sender, EventArgs e)
{
// your normal response.Redirect code here
}
So, the Page.ClientScript will fire the javascript client side, which will delay and fire the hidden button click, firing the onclick event in codebehind.
This has not been heavily used in production, but I have used the hidden button fired with javascript for years now, and it has been solid and reliable.
精彩评论