How to redirect to another page after a delay

I have a sign-in box in my webpage which is inside an 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>

Once the user is validated successfully, I want to show a message and redirect after a delay (let's say 5 seconds). I have the following code but it is not redirecting:

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 updated but the page is not redirecting for some reason.

Please help me resolve the issue.


你可以用延迟写一段Javascript代码,然后用这段代码重定向到页面

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);
}

first create a function that makes the action you need (redirect to page for example)

Second add timer to your markup and set the time interval to 5000 ( 5 sec) and mark the timer as enabled=false so the timer wont start after the page loading

once the user is validated successfully, show the message you want then enable the timer


There are many ways to do this but I love to use this code because it works well when used in many different circumstances. This is with a 5 second delay.

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);
链接地址: http://www.djcxy.com/p/71544.html

上一篇: 如何在ASPX页面上弹出确认框

下一篇: 如何在延迟后重定向到其他页面