发布News时,将News 内容保存到数据库,怎样在浏揽页面上保持输入时的文本格式.

解决方案 »

  1.   

    你的问题其实就是把字符串格式转换为html识别的格式
    首先,保存到数据库的内容必须是要有格式的(一般都有)
    c#:
    string showTxt(string strPath)
    {
    string s = "";
    string strGo=""; if (File.Exists(strPath)) 
    {
    StreamReader sr=new StreamReader(strPath,Encoding.Default); while ((s = sr.ReadLine()) != null) //sr.Peek() >= 0
    {
    strGo=strGo+s+"\r";
    }
    sr.Close();
    }
    strGo=strGo.Replace(" ","&nbsp;").Replace("\r","<br>");
    return strGo;
    }
    this.Label1.Text=@showTxt(strPath);客户端:
    <asp:Label id="Label1"style="WORD-BREAK:break-all"runat="server"Width="568px"Height="100px"
    ForeColor="DarkBlue" Font-Size="XX-Small" Font-Names="宋体" BorderStyle="None"></asp:Label>
      

  2.   

    我是把数据保存为文本文件,读取时做了一些处理( strGo=strGo+s+"\r";)
    从数据库读取也是一样,关键就是 strGo=strGo.Replace(" ","&nbsp;").Replace("\r","<br>")
    把空格换成"&nbsp;",换行换成"<br>",显示用lable,使用宋体字,效果最好。
      

  3.   

    对输入的内容进行编码:
    string s = System.Web.HttpUtility.HtmlEncode(TextBox1.Text);
    将s保存到数据库,查看时取出:
    Label1.Text = s;