简体   繁体   中英

Stop Characters from Encoding in HTML from C# Code Behind

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM