Encoding Query String Values Between .ASPX Pages
I ran into an instance today where a column’s data contained the ampersand (&) character when I hadn’t expected it to. Since the ampersand is used to separate Query String name/value pairs in the URL, that ampersand was causing me problems.
In general, you should always encode Query String values just in case they might contain a reserved character. Here’s the code snippet from my page (In another section of my XSL, I pass the selected value on the Query String.):
<option>
<xsl:if test=”@Business_x0020_Group = $Group”>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:if>
<xsl:attribute name=”value”>
<xsl:value-of select=”ddwrt:UrlEncode(string(@Business_x0020_Group))” />
</xsl:attribute>
<xsl:value-of select=”@Business_x0020_Group” />
</option>
As you can see in the bolded line, the ddwrt:UrlEncode function takes care of things for me. Note that you must do the explicit type conversion to string or you will get an error. (This is the case with most of the ddwrt namespace functions.) In the case where I had a Business Group with the string ‘[RR&C]’ in it, the ddwrt:UrlEncode function returned ‘%5bRR%26C%5d’, which can be passed as a Query String value with no issues.
<ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
<ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
<ParameterBinding Name="dvt_1_form_insertmode" Location="Postback;Connection"/>
<ParameterBinding Name="ListID" Location="None" DefaultValue="7C3A3194-98D0-4D02-B8E7-FB2E0FFCAE6A"/>
<ParameterBinding Name="Customer" Location="QueryString(Customer)" DefaultValue=""/>
<ParameterBinding Name="CustID" Location="QueryString(CustID)" DefaultValue=""/>
</ParameterBindings>