onClick in transformation

Kelly Shepard asked on April 20, 2018 23:56

Trying to include an onClick event into an ahref but can't seem to get the right formatting. Any guidance is much appreciated.

<%# !String.IsNullOrEmpty(CookieHelper.GetValue("somevalue")? "<a onclick=ga('send', 'event', 'Resource', 'Download', '" + Eval("Title") + "'); target=_blank rel=noopener class=basic-link tabindex=0 href=" + Eval("External_Link") + " >" + Eval("CTA") + " <span class='glyphicon glyphicon-menu-right' aria-hidden='true'>" + "": "" %>

Correct Answer

Peter Mogilnitski answered on April 21, 2018 12:53

Your span and a are not closed

   <%# 
    string.IsNullOrEmpty(CookieHelper.GetValue("somevalue"))? "" :
    " <a onclick=\"ga('send', 'event', 'Resource', 'Download', '" + Eval("Title") + "');\" " + 
    " target='_blank' rel='noopener' class='basic-link' tabindex='0' " + 
    " href='"+ Eval("External_Link") + "'>" + Eval("CTA") + 
    " <span class='glyphicon glyphicon-menu-right' aria-hidden='true'></span></a>"
    %>
0 votesVote for this answer Unmark Correct answer

Recent Answers


Prashant Verma answered on April 21, 2018 02:51

Hi Kelly,

Best way to write such type of transformation using String.Format instead of concatenate the string with ++. See below solution to format your string properly.

<%# !String.IsNullOrEmpty(CookieHelper.GetValue("somevalue")?String.Format("<a onclick='ga('send','event', 'Resource','Download','{0}');' target='_blank' rel='noopener' class='basic-link' tabindex='0' href='{1}'>{2}<span class='glyphicon glyphicon-menu-right' aria-hidden='true'>,Eval("Title"),Eval("External_Link"),Eval("CTA"),): "" %>

Thanks

Happy to help

4 votesVote for this answer Mark as a Correct answer

Kelly Shepard answered on April 21, 2018 04:00

Thank you for your help Prashant. The browser renders the link incorrectly still:

<a onclick="ga(" send','event',="" 'resource','download','boost="" patient="" satisfaction="" &="" hcahps="" scores="" with="" education');'="" target="_blank" rel="noopener" class="basic-link" tabindex="0" href="https://go.pardot.com/l/152471/2018-02-27/kh7cfm">Download Infographic<span class="glyphicon glyphicon-menu-right" aria-hidden="true">

I was getting an error with your original solution and needed to make a slight change:

<%# !String.IsNullOrEmpty(CookieHelper.GetValue("somevalue"))?String.Format("<a onclick='ga('send','event', 'Resource','Download','{0}');' target='_blank' rel='noopener' class='basic-link' tabindex='0' href='{1}'>{2}<span class='glyphicon glyphicon-menu-right' aria-hidden='true'>",Eval("Title"),Eval("External_Link"),Eval("CTA")): "" %>

Any thoughts on how to prevent the onclick from that jumbled mess?

Thanks again...

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on April 21, 2018 04:09

Hi Kelly,

You need to replace this ="" with ='' it's breaking you onclick function format thw valid string.

Thanks Happy to help

2 votesVote for this answer Mark as a Correct answer

Kelly Shepard answered on April 21, 2018 04:18

<%# !String.IsNullOrEmpty(CookieHelper.GetValue("somevalue"))?String.Format("<a onclick='ga('send','event', 'Resource','Download','{0}');' target='_blank' rel='noopener' class='basic-link' tabindex='0' href='{1}'>{2}<span class='glyphicon glyphicon-menu-right' aria-hidden='true'>",Eval("Title"),Eval("External_Link"),Eval("CTA")): "" %>

I am not sure I know where you are talking about. The onclick= does have a single quote and not double

0 votesVote for this answer Mark as a Correct answer

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