簡體   English   中英

如何在asp.net 4.5 Web表單中發回URL后進行更改

[英]How to change after postback URL in asp.net 4.5 web forms

這是一個執行回發的按鈕

        <asp:LinkButton runat="server" ID="btnBestPrice"  OnClick="btnSearchBestPrice_Click">Search Best Price</asp:LinkButton>

假設在頁面上單擊了此按鈕

http://localhost:47207/Default?ClusterId=131

現在回發完成后,頁面仍然

http://localhost:47207/Default?ClusterId=131

但是,回發后,我想使URL為

http://localhost:47207/Default

那可能嗎?

如果我進行重定向,則回發事件將丟失。 我仍然想完美處理回發事件。 所以,如果我能以某種方式將回發URL設置為客戶端頁面的原始URL還是?

ASP.NET 4.5 Web表單C#

我認為您的回發正在向用戶顯示一些信息,但是必須更改URL而不會中斷該過程。

首先,您必須了解,如果您在服務器端更改了URL,則瀏覽器會將其視為新頁面並發出新請求。 Response.Redirect基本上是告訴瀏覽器是時候轉到另一個頁面了。 因此,您不能在保留同一請求的同時更改URL。 (而Server.Transfer保留在相同的URL,但頁面不在您想要的位置)

因此,我為您提供了2種解決方案,以下一種對我來說很有意義,但是仍然可以重定向頁面:

protected void Page_Load(object sender, EventArgs e) {
    if (Session["ClusterId"] != null) {
        try {
            int ClusterId = int.Parse(Session["ClusterId"]);
            // Code here
        } catch { }
        Session.Remove("ClusterId");
        return;
    }
}

protected void btnSearchBestPrice_Click(object sender, EventArgs e) {
    int ClusterId = int.Parse(Request["ClusterId"]);
    Session.Add("ClusterId", ClusterId.ToString());
    Response.Redirect("~/Default");
}

這是另一種解決方案,它可以執行btnSearchBestPrice_Click事件中的所有操作,而無需重定向和會話,並綁定JavaScript頁面就緒事件, 調用history.pushState並清除表單元素的action屬性中不必要的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM