In this tutorial you will learn how to redirect user after some time delay in asp.net using c#. Normally people use redirection process after showing some message to their users, for example a message can be "Thank you for visiting our website" or after getting feedback from user you can display a message "Thank you for providing us your valuable feedback" and then redirect user to another page. It's just up to you in which scenario you want to use it. In asp.net normally we have three options to redirect user after some time delay.
1) Use javascript code in your c# code
2) Use Meta tag in your c# code
3) Use Response.AddHeader().
Redirection after some time delay in asp.net using c#
Let's have a look over these methods1) Use javascript function in your c# code.
I have already written a post related how to write and execute the javascript code from c# code behind. You just have to put your javascript code in c# code behind and that's it. Let’s have a look over example given below
Yourpage1.aspx
<asp:Button ID="btnRedirect" runat="server" Text="Redirect" OnClick="btnRedirect_Click" />In my .aspx page I have an asp:button control and using it’s onClick event I am redirecting user.
Yourpage1.aspx.cs
protected void btnRedirect_Click(object sender, EventArgs e) { string redirectionScript = "<script language='javascript'>" + "function Delayer(){"+ "setTimeout('Redirection()', 5000);"+ "}"+ "function Redirection(){" + "window.location = 'yourpage2.aspx';" + "}" + "Delayer()" + "</script>"; Page.RegisterStartupScript("Startup", redirectionScript); }It will redirect user to yourpage2.aspx after 5 seconds
2) Use meta tag in your c# code
I have already written a post for Implementing meta tags with master page in asp.net using c#. Let’s have a look over how to redirect user after some time delay by using meta tag in c# code behind.
protected void btnRedirect_Click(object sender, EventArgs e) { HtmlMeta equiv = new HtmlMeta(); equiv.HttpEquiv = "refresh"; equiv.Content = "5; url= yourpage2.aspx"; Header.Controls.Add(equiv); }OR
protected void btnRedirect_Click(object sender, EventArgs e) { Page.Header.Controls.Add(new LiteralControl("<meta http-equiv='refresh' content='5; url=yourpage2.aspx'/>")); }
It will redirect user to yourpage2.aspx after 5 seconds.
3) Use Response.AddHeader()
Use c# built-in method AddHeader() of Response Class to redirect user after some time delay. Let's have a look over how to do so
protected void btnRedirect_Click(object sender, EventArgs e) { Response.AddHeader("REFRESH", "5;URL=yourpage2.aspx"); }
It will redirect user to yourpage2.aspx after 5 seconds.
This AddHeader() function takes two Parameters. First one is HeaderName which is a string indicating the name of the new header. The second parameter is HeaderValue which is a string that indicates the initial value of the new header. It doesn't return any value.
So that's it.
Happy Coding
2 comments:
Thanks a lot! Just what I needed right now! ;-)
Simple and easy.
// Lars
That was a very good article. Thanks. Emile Monestime
Post a Comment