有什么办法可以在回帖时清除查询字符串参数吗?

问题描述 投票:11回答:12

我有一个表单,有时会被链接到一些查询字符串参数。 问题是,当我发回表单时,查询字符串参数仍然存在。 我的设置方式并不是什么问题,但我就是不喜欢它在那里,如果你需要按照一定的顺序检查输入,就会发现这是一个问题。

有没有一种方法可以简单、干净地清除那个查询字符串参数? 我知道我可以改变按钮上的PostBackURL,但这似乎不太有效。

asp.net postback query-string postbackurl
12个回答
12
投票

没有,我还没有看到不通过重定向就能把它清除掉的方法。


7
投票

我想你已经回答了自己的问题。使用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"];
    }
}

6
投票

把这个放在你页面的底部?

<script language="javascript" type="text/javascript">
    document.forms[0].action = window.location.pathname;
</script>

4
投票

我也遇到了同样的问题。

我使用以下代码块解决了这个问题。

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

3
投票

你可以在某些情况下使用 Server.Transfer() 方法,该方法有一个重载,允许你指定是否应该保留querystring和表单数据。


3
投票

我假设你因为某些原因不能依赖Page.IsPostBack?

如果你做的是服务器端,那么很简单,在你的方法(Page_Load,OnInit等)中添加一个IsPostBack的检查,如果不是post back(即初始请求),则只处理querystrings。


3
投票

我刚刚也遇到了同样的问题,在网上查了一下,我找到了这个片段。

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()。当页面发回后,参数就没有了。

原文在这里。


0
投票
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");

0
投票

这个方法不是你所说的重定向,但它肯定会清除查询字符串.只需在你的JavaScript代码末尾添加这一行。

window.location.search = "";

或者...

将其添加到你的HTML页面的末尾。

<script type="text/javascript">
    window.location.search = "";
</script>

根据你插入代码的位置, 擦除查询字符串的时间会改变。


0
投票
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");

0
投票

你可以尝试 Response.Redirect(Request.CurrentExecutionFilePath());它为我工作。


0
投票

当javascript代码被执行时,它将清理网址

<script language="javascript" type="text/javascript">
   var urlWithoutParams = location.protocol + "//" + location.host + location.pathname;

    function clearCurrentURL(){
        window.history.replaceState({}, document.title, urlWithoutParams);
    }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.