0

I'm sure I'm over looking something simple, but I can't remember to save my life how to change a "GET" variable's value.

For example, say I have the web address www.food.com/food.aspx?foodType=Healthy

I know Request.QueryString["foodType"] can be used to retrieve the value, but if I want to say, change "Healthy" to "Unhealthy", how would I change the value on post back?

I'm using a on-click event right now for my button, but I am stuck on changing the value of "foodType." I tried using Request.QueryString.Add("foodType", "UnHealthy"); but that did not work.

Any ideas?

snapplex
  • 851
  • 3
  • 13
  • 27
  • Not sure if this is what your looking for but might help. If not I'll delete this comment http://stackoverflow.com/questions/3813934/change-single-url-query-string-value – Ryan Beaulieu Jan 25 '13 at 14:15
  • are they calling the same url? Take note that Post method cannot be called by Get method – Jobert Enamno Jan 25 '13 at 14:18

2 Answers2

1

To my knowledge, you can't; the collection is read-only and only exists on returning the response as a by-product of the original request. You could use Response.Redirect(url), where url is the original URL with a value changed - this will then re-issue a request and the server will get a chance to 'use' the new variable and serve the user with the result (directed to the new URL, even if the page is the same).

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

I'm not sure what your use case is, but you cannot modify the collection. As @Grant mentioned already the collection is read only.

If you use look at the source for HttpRequest it creates the QueryString collection like this:

new HttpValueCollection(_queryStringText, true, true, Encoding.Default); 

That second parameter sets the collection as read only.

Josh
  • 44,706
  • 7
  • 102
  • 124