【发布时间】:2009-05-12 17:26:40
【问题描述】:
我有一个有时会与一些查询字符串参数链接的表单。问题是当我回发表单时,查询字符串参数仍然存在。我设置它的方式并不是真正的问题,但我只是不喜欢它在那里,如果您需要按特定顺序检查输入,它可能会出现问题。
有没有办法以一种简单、干净的方式清除该查询字符串参数?我知道我可以更改按钮上的 PostBackURL,但这似乎不太有效。
【问题讨论】:
标签: asp.net postback query-string postbackurl
我有一个有时会与一些查询字符串参数链接的表单。问题是当我回发表单时,查询字符串参数仍然存在。我设置它的方式并不是真正的问题,但我只是不喜欢它在那里,如果您需要按特定顺序检查输入,它可能会出现问题。
有没有办法以一种简单、干净的方式清除该查询字符串参数?我知道我可以更改按钮上的 PostBackURL,但这似乎不太有效。
【问题讨论】:
标签: asp.net postback query-string postbackurl
不,我还没有看到没有重定向的方法来清除它。
【讨论】:
我想你已经回答了你自己的问题。使用 PostBackURL 属性。
<asp:Button PostBackUrl='<%# Request.ServerVariables["URL"] %>' runat="server" Text="Submit" />
或者类似的东西
foreach (Control ctrl in Page.Controls)
{
if (ctrl is Button)
{
((Button)ctrl).PostBackUrl = Request.ServerVariables["URL"];
}
}
【讨论】:
把它放在页面底部?
<script language="javascript" type="text/javascript">
document.forms[0].action = window.location.pathname;
</script>
【讨论】:
我遇到了同样的问题。
我使用以下代码块解决了它:
private void ClearQueryString()
{
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
isreadonly.SetValue(this.Request.QueryString, false, null);
this.Request.QueryString.Remove("Action");
this.Request.QueryString.Remove("param1");
this.Request.QueryString.Remove("paramN");
}
【讨论】:
在某些情况下,您可以使用Server.Transfer() 方法,该方法有一个重载,允许您指定是否应保留查询字符串和表单数据。
【讨论】:
我认为由于某种原因您不能依赖 Page.IsPostBack?
如果您正在做的是服务器端,那么在您的方法(Page_Load、OnInit 等)中添加对 IsPostBack 的检查很简单,并且仅在不是回发(即初始请求)时才处理查询字符串.
【讨论】:
我刚刚遇到了同样的问题,在网上搜索了一番后,我发现了这个 sn-p:
public void ClearQueryStrings()
{
string clientCommand = string.Format(
"document.forms[\"{0}\"].action = \"{1}\";",
this.Form.Name, Request.Url.Segments[Request.Url.Segments.Length - 1]);
ClientScript.RegisterStartupScript(this.GetType(), "qr", clientCommand, true);
}
在处理完原始查询字符串参数后,我在 Page_Load 中调用 ClearQueryStrings()。当页面回传时,参数就没有了。
【讨论】:
using System.Collections;
using System.Reflection;
使用以下代码清除查询字符串参数。
// To clear Query string value
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);
// remove
this.Request.QueryString.Remove("YourQueryStringParameter");
【讨论】:
这个方法不是你所说的重定向,但它肯定会删除查询字符串。 只需将此行添加到 JavaScript 代码的末尾即可:
window.location.search = "";
或者
将此添加到 HTML 页面的末尾:
<script type="text/javascript">
window.location.search = "";
</script>
根据插入代码的位置,擦除查询字符串的时间会有所不同。
【讨论】:
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);
// remove
this.Request.QueryString.Remove("status");
【讨论】:
你可以试试Response.Redirect(Request.CurrentExecutionFilePath());
它对我有用。
【讨论】:
当 javascript 代码被执行时,它会清理 url
<script language="javascript" type="text/javascript">
var urlWithoutParams = location.protocol + "//" + location.host + location.pathname;
function clearCurrentURL(){
window.history.replaceState({}, document.title, urlWithoutParams);
}
</script>
【讨论】: