简体   繁体   English

C#和JavaScript-转义单引号字符

[英]C# and JavaScript - Escaping Single Quote Character

I have an XML file storing a string like the following: 我有一个XML文件,存储的字符串如下所示:

Error: The form's name field was left empty!

Now, this message in the XML file is used in various locations. 现在,XML文件中的此消息已在各个位置使用。 For instance, it is sometimes bound to an aspx control and sometimes displayed in a JavaScript alert. 例如,它有时绑定到aspx控件,有时显示在JavaScript警报中。

In order to escape the single quote character, I modified the message as follows: 为了转义单引号字符,我对消息进行了如下修改:

Error: The form\'s name field was left empty!

When the message is shown in a JavaScript alert, it is displayed as it should with the single quote escaped. 当消息显示在JavaScript警报中时,将按原样显示,并转义单引号。 However, when it is bound to an aspx control, it is displayed as is. 但是,将其绑定到aspx控件时,将按原样显示。

I tried modifying the message as follows: 我尝试修改消息,如下所示:

Error: The form's name field was left empty!

and

Error: The form‘s name field was left empty!

With these two cases, the message displays as it should when bound to an asp control. 对于这两种情况,该消息在绑定到asp控件时应显示为应有的状态。 However, when passed to a JavaScript alert, I get the ") expected" error in IE8. 但是,当传递给JavaScript警报时,我在IE8中收到“)预期”错误。

How can I get this to work for both aspx controls and JavaScript alerts please? 我如何才能使它同时适用于aspx控件和JavaScript警报? I have already wasted a lot of time on it. 我已经浪费了很多时间。 Thank you so much. 非常感谢。

NB I am using a very old version of the .NET Framework, specifically 2.0. 注意:我使用的是.NET Framework的非常旧的版本,特别是2.0。

You should just store your messages verbatim and then let each client handle the special character. 您应该只逐字存储消息,然后让每个客户端处理特殊字符。 So, store as Error: The form's name field was left empty! 因此,将其存储为Error: The form's name field was left empty! and then within each language add a step that checks for special characters and escapes them accordingly. 然后在每种语言中添加一个步骤,以检查特殊字符并相应地对其进行转义。

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

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