Monday, October 17, 2011

Modify Query String


If you build a hyperlink on a web page dynamically, sometimes you may need to append or modify an existing query string.
To modify a url with an existing query string, first grab the AbsoluteUrl property:

string path = Request.Url.AbsolutePath ;

This will give you a url wihout the query string portion.

If you need to reuse an existing portion of the query string, you have to get it yourself, e.g:

string key = Request.QueryString["key"];
string path = Request.Url.AbsolutePath + "?key="+ key;


Now, you can build your url:

string url  = path+ "&key2="+ myValue;

Related posts:
Add JavaScript Dynamically to ASP.NET UpdatePanel
Examine Columns in ASP.NET Dynamic Data MetaTable




1 comment: