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.

4 Comments

  1. Renee:
     
    From the looks of it, you are doing things right.  What does the URL look like when you are loading this page?  Does the textbox get any value or does it remain blank?  You might want to just try displaying the values of your parameters to see what they have in them.  Just something like: <xsl:value-of select="$Customer" />
     
    M.
    Reply
  2. Thanks Marc-
     
    I see where building the URL dynamically is a great idea – thanks.  The problem I am still having is when I click the URL in the column to go to the TouchForm.aspx, the parameters don’t seem to be getting picked up there.  They do get picked up however, when the ‘&’ doesn’t change to ‘&amp;’ .  Am I missing something, or have incorrect code somewhere?  I do very much appreciate your help, I’ve been working at this for days now.  I am a very beginning SharePoint dev/hacker :)
     
    This is how I set up getting the Parameter in the TouchForm.aspx (in bold):
    <ParameterBindings>
      <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>
     
    This is how I’m trying to insert it in the XSL:
        <asp:TextBox runat="server" id="ff1{$Pos}" __designer:bind="{ddwrt:DataBind(‘i’,concat(‘ff1′,$Pos),’Text’,’TextChanged’,’ID’,ddwrt:EscapeDelims(string(@ID)),’@Title’)}" text="{$Customer}" />
     
    <asp:TextBox runat="server" id="ff5{$Pos}" __designer:bind="{ddwrt:DataBind(‘i’,concat(‘ff5′,$Pos),’Text’,’TextChanged’,’ID’,ddwrt:EscapeDelims(string(@ID)),’@Reference_x0020_Account_x0020_ID’)}" text="{$CustID}" Visible="False" /> 
     
    Many Thanks,
    Renee
    Reply
  3. Renee:
     
    If your column contains a URL with a Query String, then you will need to write your XSL properly to deal with it.  It probably needs to be something like this (but it’s hard to know exactly without seeing the details of your implementation):
    <a href="{@URLColumn}">Text to click</a>
     
    That said, generally when I see folks storing full URLs with Query Strings on them in a column, they are not clear on the power of the DVWP.  You can build URLs dynamically using the values of columns in the list item(s) in your XSL.  This might look something like this:
    where $URL is a parameter defined to be the value of the Server Variable ‘URL’.
     
    Hope this helps…
     
    M.
    Reply
  4. Hello – What code please would I use if I didn’t want a ‘&’ in the querystring to be converted to either ‘%26’ or ‘&amp;’
    I need to pass a querystring exactly as this:
     
    and the ‘&’ keep getting converted to ‘&amp;’ which won’t work for what I need.
     
    I’m using a MOSS dataview and one of my columns value is a URL w/querysting, when clicking on the URL I want to open a new dataform and pass a value to a text box from the querystring parameter.
     
    Thanks!
    Renee
    Reply

Have a thought or opinion?