[英]How to redirect to another page after a delay
我的網頁中有一個登錄框,它位於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>
成功驗證用戶后,我想顯示一條消息並在延遲后重定向(假設為 5 秒)。 我有以下代碼,但它沒有重定向:
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");
}
消息已更新,但由於某種原因頁面未重定向。
請幫我解決問題。
您可以延遲編寫一個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);
}
首先創建一個函數,使您需要的操作(例如重定向到頁面)
第二次為您的標記添加計時器並將時間間隔設置為5000(5秒)並將計時器標記為enabled = false,以便計時器在頁面加載后不會啟動
成功驗證用戶后,顯示所需的消息,然后啟用計時器
有很多方法可以做到這一點,但我喜歡使用這個代碼,因為它在許多不同的情況下使用時效果很好。 延遲時間為5秒。
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);
Response.AppendHeader("刷新", "2;url=page.aspx");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.