当前位置: 代码迷 >> ASP.NET >> 何如在文本框中展示一些特殊的文本
  详细解决方案

何如在文本框中展示一些特殊的文本

热度:4872   发布时间:2013-02-25 00:00:00.0
何如在文本框中显示一些特殊的文本?
前台.aspx中有如下代码:

<input type="text" id="sm" value='<%= HttpUtility.HtmlEncode(shuoMing) %>' />

shuoMing是个字符串,里面可能含有单引号或双引号或其他的特殊符号,可能会和value='' 的定界符冲突。

HttpUtility.HtmlEncode确实进行了编码,但没有对引号编码,还有无其他的编码方法???谢谢啦!!!

value='\'' 这样是不行的。

------解决方案--------------------------------------------------------
是对引号进行编码的
方法1
使用双引号
<input type="text" id="sm" value="<%= HttpUtility.HtmlEncode(shuoMing) %>" />

方法2:
<input type="text" id="sm" value='<%= HttpUtility.HtmlEncode(shuoMing).Replace("'","&apos;") %>' />


方法3:
<input type="text" id="sm" value='<%= HttpUtility.HtmlEncode(shuoMing).Replace("'","&#39;") %>' />
  相关解决方案