简体   繁体   English

停止字符从 C# 代码隐藏在 HTML 中编码

[英]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.我正在从后面的 C# 代码生成控件,并希望应用一个 onclick 属性,该属性将发送一个模式来阻止显示。 When the code executes, apostrophes in the onclick function string are encoded to '当代码执行时,onclick 函数字符串中的撇号被编码为' 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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