Tuesday, August 18, 2009

Updating Query String values

Scenario:
I have done it many time with different ways, hopefully this is easiest way and i am blogging it , I can stick to it for time longer without rediscovering the wheel again :-)

Solution:
Using NameValueCollection object.

Code:

using System.Collections.Specialized;

NameValueCollection variables = new NameValueCollection(this.Page.Request.QueryString);

string queryParameter1 = "ID";
if (!string.IsNullOrEmpty(variables[queryParameter1]))
variables.Remove(queryParameter1);

variables.Add(queryParameter1, fieldName);

string queryString = string.Empty;
foreach (String s in variables)
{
queryString += s + "=" + variables[s] + "&";
}

string newUrl = string.Format("{0}?{1}",this.Page.Request.Path,queryString)

0 comments: