11

I have a form that sometimes gets linked to with some query string parameters. The problem is that when I post back the form, the query string parameter is still there. It's not really an issue the way I have it setup, but I just don't like it being there, and could see it being a problem if you needed to check for input in a certain order.

Is there a way to clear that query string parameter in an easy, clean way? I know I could change the PostBackURL on the button, but that doesn't seem too efficient.

Alex Angas
  • 59,219
  • 41
  • 137
  • 210
John B
  • 20,062
  • 35
  • 120
  • 170

12 Answers12

12

No, I haven't seen a way to clear it out without a redirect.

rvarcher
  • 1,566
  • 1
  • 12
  • 14
  • Is there a clean way to do a Response.Redirect() to the page you're currently on? – John B May 12 '09 at 18:08
  • 3
    Try: Response.Redirect(Request.CurrentExecutionFilePath()) – rvarcher May 12 '09 at 19:03
  • I know this is really old, but CurrentExecutionFilePath is a property, so you shouldn't have the parentheses at the end. Maybe consider adding your comment to your answer to make it more complete? – Zack Aug 19 '14 at 13:11
7

I think you've answered your own question. Use the PostBackURL property.

<asp:Button PostBackUrl='<%# Request.ServerVariables["URL"] %>' runat="server" Text="Submit" />

Or something like

foreach (Control ctrl in Page.Controls)
{
    if (ctrl is Button)
    {
        ((Button)ctrl).PostBackUrl = Request.ServerVariables["URL"];
    }
}
Axl
  • 8,272
  • 2
  • 26
  • 18
6

put this at the bottom of your page?

<script language="javascript" type="text/javascript">
    document.forms[0].action = window.location.pathname;
</script>
Al W
  • 7,539
  • 1
  • 19
  • 40
5

I was stuck with same issue.

I solved it using the following code block:

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");
}
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
bgS
  • 219
  • 3
  • 5
3

I just came across the same problem and after some hunting on the Web I found this snippet:

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

I call ClearQueryStrings() in my Page_Load after I am done processing the original query string parameters. When the page posts back, the parameters are gone.

Original article is here.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Miguel
  • 33
  • 3
3

You can in some cases use the Server.Transfer() method, which has an overload that allows you to specify whether the querystring and form data should be preserved or not.

janhartmann
  • 14,713
  • 15
  • 82
  • 138
3

I assume that you can't rely on Page.IsPostBack for some reason?

If what you're doing is server-side, then it's simple to add a check for IsPostBack in your methods (Page_Load, OnInit, etc) and only processing the querystrings if it's not a post back (i.e. the initial request).

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
0
using System.Collections;
using System.Reflection;

Use the following code to Clear Query String Parameters.

// 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");
António Almeida
  • 9,620
  • 8
  • 59
  • 66
0

This method is not what you would call a redirect, but it definitely will erase the query string. Simply add this line to the end of your JavaScript code:

window.location.search = "";

Or

Add this to the end of your HTML page:

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

Depending on where you insert the code, the timing of erasing the query string will change.

royn1
  • 33
  • 5
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

You Can try Response.Redirect(Request.CurrentExecutionFilePath()); Its working for me.

amit
  • 429
  • 5
  • 8
  • 20
0

When the javascript code is executed it will clean the url

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

    function clearCurrentURL(){
        window.history.replaceState({}, document.title, urlWithoutParams);
    }
</script>