[英]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.