Everyplace I looked for a solution to the QueryString.Clear() problem (that is, you can’t clear the querystring in ASP.NET programmatically) proclaimed that it couldn’t be done without using some screwy client-based redirection code.

Well, they were partially right: it does take some client-based code, but it, in my opinion, isn’t screwy or ghetto (as one person called it–whom I also pilfered the idea from). It is, in fact, pretty elegant and points to the raw power in ASP.NET.

Here’s the code:

public void ClearQueryStrings()
{
    string clientCommand = string.Format(
     
"document.all(\"{0}\").action = \"{1}\";",
      this.Form.Name, Request.Url.Segments[Request.Url.Segments.Length - 1]);
    ClientScript.RegisterStartupScript(this.GetType(), "qr", clientCommand, true);
}

Enjoy!