0

Hello and thank you in advance. I am generating controls from C# code behind and want to apply an onclick attribute that will sent a modal to block display. When the code executes, apostrophes in the onclick function string are encoded to ' and I want to stop that from happening and pass the raw text.

I have tried the following to solve the problem with no resolution:
\'
''
<%= myString %>
HtmlString(myString).ToHtmlString();

There are probably other iteration that i tried but I lost track of them all.

This is the code section creating the control:

HtmlGenericControl viewMoreInfo = new HtmlGenericControl("span");
//HtmlString onclick1 = new HtmlString($"document.getElementById('{i.il_itemcode}Modal').style.display = 'block'");
string onclick1 = $"document.getElementById('{i.il_itemcode}Modal').style.display = 'block'";
viewMoreInfo.Attributes["onclick"] = onclick1;
viewMoreInfo.Attributes["class"] = "w3-bar-item w3-button w3-white w3-xlarge w3-right";
viewMoreInfo.InnerText = "+";

Here is an example of what it renders as:

<span onclick="document.getElementById(&#39;TEST1Modal&#39;).style.display = &#39;block&#39;" class="w3-bar-item w3-button w3-white w3-xlarge w3-right">+</span>

I need the apostrophe to render correctly for the function to work.

Any help would be greatly appropriated.

1
  • 1
    That should work as it currently is. Feb 18, 2020 at 15:53

1 Answer 1

2

Your question is misleading because "I need the apostrophe to render correctly for the function to work." is incorrect. Note the following which is an exact copy of your example, and the click event works just fine as it currently being rendered.

<p id="TEST1Modal" style="display:none">This is hidden until clicked</p>
<span onclick="document.getElementById(&#39;TEST1Modal&#39;).style.display = &#39;block&#39;" class="w3-bar-item w3-button w3-white w3-xlarge w3-right">+</span>

1
  • You are absolutely correct. Thank you for pointing that out. The error was in a different section and I thought the HTML render would not run properly. Thank you. Feb 18, 2020 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.