<p>I'm implementing a custom control and in this control I need to write a bunch of links to the current page, each one with a different query parameter. I need to keep existing query string intact, and add (or modify the value  of ) an extra query item (eg. "page"):</p>

<pre><code>"Default.aspx?page=1"
"Default.aspx?page=2"
"Default.aspx?someother=true&amp;page=2"
</code></pre>

<p>etc.</p>

<p>Is there a simple helper method that I can use  in the Render method  ... uhmm ... like:</p>

<pre><code>Page.ClientScript.SomeURLBuilderMethodHere(this,"page","1");
Page.ClientScript.SomeURLBuilderMethodHere(this,"page","2");
</code></pre>

<p>That will take care of generating a correct URL, maintain existing query string items and not create duplicates  eg. page=1&amp;page=2&amp;page=3?</p>

<p>Rolling up my own seems like such an unappealing task.</p>

I'm afraid I don't know of any built-in method for this, we use this method that takes the querystring and sets parameters
```
```
/// <summary>
    /// Set a parameter value in a query string. If the parameter is not found in the passed in query string,
    /// it is added to the end of the query string
    /// </summary>
    /// <param name="queryString">The query string that is to be manipulated</param>
    /// <param name="paramName">The name of the parameter</param>
    /// <param name="paramValue">The value that the parameter is to be set to</param>
    /// <returns>The query string with the parameter set to the new value.</returns>
    public static string SetParameter(string queryString, string paramName, object paramValue)
    {
        //create the regex
        //match paramname=*
        //string regex = String.Format(@"{0}=[^&]*", paramName);
        string regex = @"([&?]{0,1})" + String.Format(@"({0}=[^&]*)", paramName);

        RegexOptions options = RegexOptions.RightToLeft;
        // Querystring has parameters...
        if (Regex.IsMatch(queryString, regex, options))
        {
            queryString = Regex.Replace(queryString, regex, String.Format("$1{0}={1}", paramName, paramValue));
        }
        else
        {
            // If no querystring just return the Parameter Key/Value
            if (queryString == String.Empty)
            {
                return String.Format("{0}={1}", paramName, paramValue);
            }
            else
            {
                // Append the new parameter key/value to the end of querystring
                queryString = String.Format("{0}&{1}={2}", queryString, paramName, paramValue);
            }
        }
        return queryString;
    }
```
```
Obviously you could use the QueryString
```
NameValueCollection
```
property of the URI object to make looking up the values easier, but we wanted to be able to parse any querystring.