简体   繁体   中英

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

Here a button that does a postback

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

Assume that this button is clicked on page

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

Now after the postback is completed, the page is still

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

However, after the postback, i want to make URL to be

http://localhost:47207/Default

Is that possible?

If i make a redirect, the postback event would be loss. I still want to process the postback event perfectly fine. So if somehow i can set the postback url to be raw url of the page on the client side or?

asp.net 4.5 web forms c#

I assume your postback is displaying some information to user, but have to change the URL without interrupt the process.

First of all you have to understand that if you changed the URL in the server side, the browser will treat it as a new page and make a new request. Response.Redirect is basically telling the browser it is time to move onto another page. So you cannot change the URL while remaining in the same request. (while Server.Transfer is remaining at the same URL but different page which is not what you want)

So I have 2 solutions for you, the following one make sense to me but there is still redirecting the page:

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

Here is another solution that does all the actions in your btnSearchBestPrice_Click event without Redirect and Session, and bind a JavaScript page ready event, call history.pushState and also wipe out the unnecessary parameters in the action attribute of your form element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM