Preserving the Existing Query String When Building a Link in a DVWP

Another question came to me in email today that I thought was worth sharing, along with my answer.  The email referred to my post about Alpha Selection of List Items in a Data View Web Part (DVWP), but it’s relevant to many Middle Tier development tasks.

I hope this email comes to you in good spirits! I was messing around with your alpha selection and it works great except I have 1 more stipulation. I filter a dvwp by URL parameter for instance:

www.mysite.com/site/Pages/thispage.aspx?Type=Type1 OR

www.mysite.com/site/Pages/thispage.aspx?Type=Type2

And then I would like to be able to them choose the A-Z and when it filters it will keep the original URL, but as of right now it will drop the Type=Type1/Type2 and while it filters ‘A’ it filters ‘A’ for both Type1 and Type2. Is there a way that I can allow it to keep the Type parameter then add the Letters parameter to the URL for it to filter further?

Sure you can!  You can just parse out the existing Query String parameters and add them back onto the URL when you build it up.  If you set up a new parameter like so:

<ParameterBinding Name="QUERY_STRING" Location="ServerVariable(QUERY_STRING)" DefaultValue=""/>

then you have the existing Query String parameters available to you.  You’ll just want to be sure to strip out the parameters you want to add if they are already there, something like this:

<a href=”{$URL}?{substring-before($QUERY_STRING, ‘&amp;Letter’)}&amp;Letter={$ThisLetter}”>

(This simple example assumes that the Letter parameter will always be there.)

Have a thought or opinion?