Clear QueryStrings in ASP.NET
code October 30th, 2007Everyplace 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!
October 30th, 2007 at 10:42 am
Well done!
July 14th, 2008 at 1:24 pm
Thanks, it worked for me.
September 30th, 2008 at 12:21 am
u might redirect the page to another page … it will clear the values or string in URl
January 27th, 2009 at 10:09 am
Nice, very nice!
However, you’ve got some cross-browser issues there. First of all, the document.all array is IE only, and pretty much only used around about the IE4 timeframe. The document.forms array should be used instead (it’s also better because it holds only the page’s forms).
The other issue is that Firefox won’t understand indexing the document.forms array with round brackets – use square brackets instead.
With these two changes in place, the final code works on IE, FF and Google Chrome (the only ones I’ve tested so far).
January 27th, 2009 at 11:16 am
Hey Andrew, thanks for that update!