/// <summary>
/// 替换字符串中的特殊字符,
/// flag = true:替换串中的大于和小于号等特殊字符为转义标记
/// flag = false:将经过转义的串,转为正常输出
/// </summary>
/// <param name="strContent"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static string ReplaceStr(string strContent,bool flag)
{
if (flag)
{
strContent=strContent.Replace("&","&amp;");
strContent=strContent.Replace("'","''");
strContent=strContent.Replace("<","&lt;");
strContent=strContent.Replace(">","&gt;");
strContent=strContent.Replace("\"","&quot;");
strContent=strContent.Replace("\r\n","<br/>");
//strContent=strContent.Replace(" ","&nbsp");
return strContent;
}
else
{
strContent=strContent.Replace("&amp;","&");
strContent=strContent.Replace("''","'");
strContent=strContent.Replace("&lt;","<");
strContent=strContent.Replace("&gt;",">");
strContent=strContent.Replace("&quot;","\"");
strContent=strContent.Replace("<br/>","\r\n");
//strContent=strContent.Replace(" ","&nbsp");
return strContent;
}
}

解决方案 »

  1.   

    一般的,保存时不用管它,
    show的时候用Server.HtmlEncode就可以了
      

  2.   

    如果你是要用在sql句中,用带参数的存储过程最好.
      

  3.   

    几位的意见都不错;
    To: cpp2017(幕白兄) 
         我直接在页面中输入保存会出错(如带'的文本),是否在页面中要导入什么东东?
    能否给个具体的实例:
         保存时,显示时,修改时就要点提示一下。
      

  4.   

    <script language="javascript">
    function check(str)
    {
      var pattern=/^([\u4e00-\u9fa5]|[0-9_a-zA-Z]){1,20}$/;
    if(!pattern.test(str))
    {
    alert('只能输入中文,字母或者数字!');
    }
    }
    </script>