回帖时有没有办法清除查询字符串参数?

时间:2009-05-12 17:26:41

标签: asp.net postback query-string postbackurl

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

有没有办法以简单,干净的方式清除查询字符串参数?我知道我可以在按钮上更改PostBackURL,但这看起来效率不高。

12 个答案:

答案 0 :(得分:12)

不,我没有看到一种方法可以在没有重定向的情况下将其清除。

答案 1 :(得分: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"];
    }
}

答案 2 :(得分:6)

将它放在页面底部?

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

答案 3 :(得分: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");
}

答案 4 :(得分:3)

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

答案 5 :(得分:3)

我认为你出于某种原因不能依赖Page.IsPostBack?

如果您正在做的是服务器端,那么在您的方法(Page_Load,OnInit等)中添加对IsPostBack的检查很简单,并且如果它不是回发(即初始请求),则仅处理查询字符串

答案 6 :(得分: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()。当页面回发时,参数消失了。

Original article is here.

答案 7 :(得分: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");

答案 8 :(得分:0)

这种方法不是你所谓的重定向,但肯定会删除查询字符串。 只需将此行添加到JavaScript代码的末尾:

window.location.search = "";

或者

将其添加到HTML页面的末尾:

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

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

答案 9 :(得分: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");

答案 10 :(得分:0)

你可以尝试SELECT commentId FROM comment_datas GROUP BY commentId HAVING sum(name = 'type' AND value = 'post') > 0 AND sum(name = 'target' AND value = '2') > 0 它为我工作。

答案 11 :(得分: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>