怎么样将输入时候的空格,换行等格式保存到数据库中,在显示的时候原样出现?

解决方案 »

  1.   

    入库时HtmlEncode
    取数据时HtmlDecode
      

  2.   

    Replace("\n","<br>")就够了~
      

  3.   

    用HtmlEncode和HtmlDecode好象不能解决空格问题,我一般用这样的方法解决。保存用以下方法,显示时直接取出显示:
    public static string HTMLEncode(string strContent)
    {
    StringBuilder rtContent = new StringBuilder( HttpUtility.HtmlEncode(strContent) );
    rtContent.Replace("\x0027", "&#39;").Replace(" ","&nbsp;").Replace("\x000a", "<BR>").Replace("\x000d", "");
    return rtContent.ToString();
    }编辑时用以下方法取出:
    public static string HTMLDecode(string strContent)
    {
    StringBuilder rtContent = new StringBuilder( strContent );
    rtContent.Replace("<BR>", "\x000a").Replace("&nbsp;"," ").Replace("&#39;", "\x0027");
    return HttpUtility.HtmlDecode( rtContent.ToString() );
    }不知是否适合你?