0

这是一个执行回发的按钮

        <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

那可能吗?

如果我进行重定向,回发事件将丢失。我仍然想完美地处理回发事件。因此,如果我可以以某种方式将回发网址设置为客户端页面的原始网址,或者?

asp.net 4.5 网络表单 c#

4

1 回答 1

0

我假设您的回发正在向用户显示一些信息,但必须在不中断进程的情况下更改 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 属性中的不必要参数。

于 2016-08-23T06:15:52.983 回答