How to redirect to another page after a delay

I have a login window to my webpage, which is inside the UpdatePanel

 <asp:UpdatePanel runat="server" ClientIDMode="Static" ID="upSign" UpdateMode="Conditional"> <ContentTemplate> <div class="dvHolder hidOverflow clearfix"> <input id="txtSUser" type="text" name="SUsername" value="" placeholder="Username" runat="server" /> </div> <div class="dvHolder hidOverflow clearfix"> <input id="txtSPass" type="password" name="SPassword" value="" placeholder="Password" runat="server" /> </div> <div class="dvHolder hidOverflow clearfix setTextRight"> <asp:Button ID="btnSignIn" ClientIDMode="Static" runat="server" Text="Sign In" OnClick="btnSignIn_Click" /> <asp:Label runat="server" Text="" ID="lblSSuccess" ClientIDMode="Static" CssClass="lblMsgSuccess" /> </div> </ContentTemplate> </asp:UpdatePanel> 

As soon as the user is successfully verified, I want to show the message and redirect after the delay (let them say 5 seconds). I have the following code, but it does not redirect:

 public void btnSignIn_Click(object sender, EventArgs e) { lblSSuccess.Text = "We found you, now redirecting..."; lblSSuccess.ForeColor = ColorTranslator.FromHtml("#037203"); Session["UseIsAuthenticated"] = "true"; Response.AppendHeader("Refresh", "5;url=homepage.aspx"); } 

The message is being updated, but for some reason the page is not being redirected.

Please help me solve the problem.

+5
source share
3 answers

You can write a delayed Javascript block and redirect to the page using this code

 public void btnSignIn_Click(object sender, EventArgs e) { lblSSuccess.Text = "We found you, now redirecting..."; lblSSuccess.ForeColor = ColorTranslator.FromHtml("#037203"); Session["UseIsAuthenticated"] = "true"; ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "redirectJS", "setTimeout(function() { window.location.replace('homepage.aspx') }, 5000);", true); } 
+7
source

first create a function that does the desired action (redirecting to a page, for example)

Second, add a timer to your markup and set the time interval to 5000 (5 seconds) and mark the timer as enabled = false so that the timer does not start after the page loads

after successful user verification, show the desired message, then turn on the timer

+2
source

There are many ways to do this, but I like to use this code because it works well when used in a variety of circumstances. This is a delay of 5 seconds.

 HtmlMeta oScript = new HtmlMeta(); oScript.Attributes.Add("http-equiv", "REFRESH"); oScript.Attributes.Add("content", "5; url='http://www.myurl.com/'"); Page.Header.Controls.Add(oScript); 
0
source

All Articles