views:

872

answers:

3

From reading here and around the net, I'm close to assuming the answer is "no", but...

Let's say I have an ASP.Net page that sometimes has a query string parameter. If the page has the query string parameter, I want to strip it off before, during, or after postback. The page already has a lot of client-side script (pure JavaScript and jQuery).

As an example, say I load:

http://myPage.aspx?QS=ABC

The QS parameter is necessary to control what appears on the page when it first loads and is set by the page that "calls" it. myPage.aspx has form elements that need to be filled in and has a submit button that does a postback. When the page completes the postback, I need to returned URL to be:

http://myPage.aspx

in order to avoid the client-side code that is called when the query string is present. In other words, after a submit I don't want the client side actions associated with the query string parameter to fire. I know I could append the form contents to the URL as query string parameters themselves and just redirect to the new URL and avoid the submit/postback, but that will require a lot more type checking on the codebehind to avoid bad data and casual spoofing. I supposed I could also set a hidden field in the codebehind and look at it along with the query string to cancel the client-side behavior if I am coming back from the postback, but that still leaves the query string intact basically forever and I want to get rid of it after the initial page load.

Any ideas or best practices?

PS - Is there anything I can do with the Form.Action property that won't break the postback behavior?

+1  A: 

It is probably bad practice, but in those cases (and ones I just need to 'reset' the page), I simply do a Response.Redirect to the same page.

leppie
IIRC, Response.Redirect is going to lose my viewstate and Server.Transfer will preserve my viewstate (even when going to an entirely different page), but will leave the URL (with the query string) intact. I'm not seeing an advantage here.
CMPalmer
Plus a considerable disadvantage of additional roundtrips
annakata
+1  A: 

iam not sure this is what you are looking for, but if understand correctly you could do this:

-on page load check for the QS value, if its not there use a hidden input field.

-first time page loads with QS, do your normal processing and store QS value in a hidden input field.

-if no QS then use the hidden input value

-after postback, you could redirect to the same page, at that point you could user Request.Form[] to retrieve the hidden input field, still load the data properly but get rid of the QS.

it made sense in my head, i am not sure it make sense now, but i'll let you decide.

Victor
+2  A: 

I would use an HTTPModule to intercept and rewrite the URL and query as it came in. I'm not 100%, but I don't believe this has any impact with viewstate.

It sounds (and looks) complex, but it's actually fairly trivial and open to a ton of refinement and extension (as per .NET in general).

annakata