简体   繁体   English

如何在asp.net 4.5 Web表单中发回URL后进行更改

[英]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 但是,回发后,我想使URL为

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? 所以,如果我能以某种方式将回发URL设置为客户端页面的原始URL还是?

asp.net 4.5 web forms c# ASP.NET 4.5 Web表单C#

I assume your postback is displaying some information to user, but have to change the URL without interrupt the process. 我认为您的回发正在向用户显示一些信息,但是必须更改URL而不会中断该过程。

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. 首先,您必须了解,如果您在服务器端更改了URL,则浏览器会将其视为新页面并发出新请求。 Response.Redirect is basically telling the browser it is time to move onto another page. Response.Redirect基本上是告诉浏览器是时候转到另一个页面了。 So you cannot change the URL while remaining in the same request. 因此,您不能在保留同一请求的同时更改URL。 (while Server.Transfer is remaining at the same URL but different page which is not what you want) (而Server.Transfer保留在相同的URL,但页面不在您想要的位置)

So I have 2 solutions for you, the following one make sense to me but there is still redirecting the page: 因此,我为您提供了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");
}

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. 这是另一种解决方案,它可以执行btnSearchBestPrice_Click事件中的所有操作,而无需重定向和会话,并绑定JavaScript页面就绪事件, 调用history.pushState并清除表单元素的action属性中不必要的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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