Capturing an Ampersand in Query String Macro

Kevin Kim-Murphy asked on September 17, 2015 16:28

In my Online Form field I am trying to capture the value of a query string parameter that may have special characters (like "&", "!", or "-").

Example:

Page URL: http://www.domain.com/pagename?parameter=Me%20&%20Julio%20Down%20by%20the%20School%20Yard

In the field's default value I have placed the Query String Macro: {?parameter?}

And when I submit the form the value captured in the field is: "Me"

My expectation is that the value captured in the field would be the full parameter: "Me & Julio Down by the School Yard"

Any advice for allowing the Query String Macro to include those special characters?

Please let me know. Thanks!

Kevin

Correct Answer

Juraj Komlosi answered on September 18, 2015 12:29

Hi Kevin,

to achieve your goal you have to transform the "SongTitle" into URL-encoded string in your transformation.

It should look like that: <a href="/pagename?parameter=<%# URLHelper.URLEncode(ValidationHelper.GetString(Eval("SongTitle"),"")) %>">Contact Us

Now if the song title contains an ampersand, e.g. "Me & Julio Down by the School Yard" it will be transformed into URL-encoded string "Me+%26+Julio+Down+by+the+School+Yard".

If you use the query macro {%parameter%} in your form fields, you should see the value you expect - "Me & Julio Down by the School Yard".

Hope it will help you.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on September 17, 2015 16:39

Seems like the best way to handle this would be following something like this:

http://stackoverflow.com/questions/16622504/escaping-ampersand-in-url

It may be easier to control the query string and encoding/decoding. Just a thought.

1 votesVote for this answer Mark as a Correct answer

Kevin Kim-Murphy answered on September 17, 2015 18:22

Joshua,

Thank you. So this does help a lot - but but I cannot actually solve the problem entirely... I am thinking that maybe the Transformation that creates the Query String itself needs to be modified. So here is my follow up question:

Is there are way to encode special characters in a Transformation to hex so that the "&" would actually be "%26"?

I was looking at the text manipulation transformation methods: <%# Eval("NewsTitle", true) %> but that did not seem to work.

The query string in my example above is actually generated via a Transformation on another page and passed in a URL of a link.

<a href="/pagename?parameter=<%# Eval("SongTitle") %>">Contact Us

Resulting in the URL: http://www.domain.com/pagename?parameter=Me%20&%20Julio%20Down%20by%20the%20School%20Yard

Per the article you posted it does work if that URL is actually: http://www.domain.com/pagename?parameter=Me%20%26%20Julio%20Down%20by%20the%20School%20Yard

Let me know if you can.

Thanks Kevin

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 17, 2015 19:06

You may be able to use this transformation method, haven't tested it, but I would think it should work for you.

<%# HTMLEncode("Sample text <br />") %>

0 votesVote for this answer Mark as a Correct answer

Kevin Kim-Murphy answered on September 17, 2015 21:39

That transformation doesn't appear to work. Maybe I am implementing it wrong - but it also seems that that transformation is designed to retain HTML characters that already exist but not convert text characters to HTML (or hex).

Been messing around with a javascript solution - but it seems that I should be able to do one of the following using some combination of Transformation syntax:

a) Strip out special characters (since the value I want to pass to the Query String is from a text field in Kentico the user can enter all sorts of special characters... like &, ', !, -, etc) as the value is placed by the transformation on the page...

OR

b) Convert special characters to HEX when pulling the value from the database.

Either way I was hoping that Kentico had a solution for this so that I don't have to build a javascript or equivilant work around.

Please let me know - if you have any suggestions.

Thanks Kevin

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.