怎样把输入的文本转换成html代码存入数据库啊

/// <summary>

  /// 替换html中的特殊字符

  /// </summary>

  /// <param name="theString">需要进行替换的文本。</param>

  /// <returns>替换完的文本。</returns>

  public string HtmlEncode(string theString)

  {

   theString=theString.Replace(">", "&gt;");

   theString=theString.Replace("<", "&lt;");

   theString=theString.Replace(" ", "&nbsp;");

   theString=theString.Replace(" ", "&nbsp;");

   theString=theString.Replace("/"", "&quot;");

   theString=theString.Replace("/'", "&#39;");

   theString=theString.Replace("/n", "<br/> ");

   return theString;

  }

怎样把页面文本输入框的大段文本内容按格式录入数据库并能从数据库读出来还原格式

 

  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
  • 结帖率:
楼主发表于:2002-10-22 17:03:44
格式仅仅指回车,   空白,   等,  
如果输入有代码,   从数据库读出来显示的时候是否会执行?   怎样处理?
 
 
回复次数: 13

  • xiaomaoxia用户头像
  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
#1楼 得分:0回复于:2002-10-22 17:16:21
我顶
 

#2楼 得分:10回复于:2002-10-22 17:18:27
首先你要确定进行例如 ' " <> 回车   空格等字符的转换这样才能写入数据库,然后显示的时候在相应的转换过来就可以了
 

  • xiaomaoxia用户头像
  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
#3楼 得分:0回复于:2002-10-22 17:22:51
example:
string   test;
string   test1;
test=test.Replace( "   ", "&nbsp)
回车怎么替换???
 

#4楼 得分:10回复于:2002-10-22 17:24:11
如果是多行文本框输入。。。在多行文本框中显示。。不需要做处理。。
如果不是在多行文本框中显示
将Server.HTMLEncode(数据库内容)中chr(13)&chr(10)替换为 <br>
将chr(32)&chr(32),替换为&nbsp;&nbsp;   用Replace

你代码指什么代码。。html的?

 

#5楼 得分:10回复于:2002-10-22 17:50:16
回车?用 " <br> "替换 "/n "就行了!
 

  • xiaomaoxia用户头像
  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
#6楼 得分:0回复于:2002-10-23 09:01:37
金卡附近: <br> &nbsp&nbsp&nbsp阿克地方军&nbsp卡隧道局负疚阿克罚款&nbsp&nbsp&nbsp&nbsp国军扩散地方军官,&nbsp劳动法进口&nbsp
<br> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp卡打击法&nbsp啊死的飞机库啊&nbsp

这是我替换后数据库中保存的内容,   把它从数据库中读出来显示在页面上的还是这样:
金卡附近: <br> &nbsp&nbsp&nbsp阿克地方军&nbsp卡隧道局负疚阿克罚款&nbsp&nbsp&nbsp&nbsp国军扩散地方军官,&nbsp劳动法进口&nbsp
<br> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp卡打击法&nbsp啊死的飞机库啊&nbsp

^_^,   这该怎么办??
 

  • xiaomaoxia用户头像
  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
#7楼 得分:0回复于:2002-10-23 09:04:46
补充:以下是显示内容的panel,html代码
<asp:panel   id= "Panel1 "   runat= "server "   Font-Names= "楷体_GB2312 "   Font-Size= "Small "   Height= "175px "   Width= "750px "   ForeColor= "Black "   HorizontalAlign= "Left "> <%=forcontent%> <FONT   face= "宋体 "   size= "3 "> </FONT> </asp:panel>

forcontent是从数据库中读出的内容变量

还有,在页面中用panel能显示出格式吗?  

  我以前没做过
 

#8楼 得分:10回复于:2002-10-23 10:01:04
在存入数据库中不要做替换,在读出数据中做替换就行了!
要不做了两次替换不久替换回来了嘛!
再说,数据就是数据,万一你的数据要显示在windows中而不是在web页面上,这不就乱套了?
 

#9楼 得分:30回复于:2002-10-23 10:05:17
This   may   help   you...
///文本转化成html文档的cs函数:TextParser.Parser(string   text,   bool   allow)

using   System;
using   System.Text;
using   System.IO;

namespace   WebSite.Support
{
public   class   TextParser
{
public   TextParser()
{
//
//   TODO:   Add   constructor   logic   here
//
}

//Method   to   parse   Text   into   HTML
public   string   Parser(string   text,   bool   allow)
{
//Create   a   StringBuilder   object   from   the   string   intput   parameter
StringBuilder   sb   =   new   StringBuilder(text)   ;
//Replace   all   double   white   spaces   with   a   single   white   space   and   &nbsp;
sb.Replace( "   ", "&nbsp; ");
//Check   if   HTML   tags   are   not   allowed
if(!allow)
{  
//Convert   the   brackets   into   HTML   equivalents
sb.Replace( " < ", "&lt; ")   ;
sb.Replace( "> ", "&gt; ")   ;
//Convert   the   double   quote
sb.Replace( "/ " ", "&quot; ");
}  
//Create   a   StringReader   from   the   processed   string   of   the   StringBuilder   object
StringReader   sr   =   new   StringReader(sb.ToString());
StringWriter   sw   =   new   StringWriter();
//Loop   while   next   character   exists
while(sr.Peek()> -1)
{
//Read   a   line   from   the   string   and   store   it   to   a   temp   variable
string   temp   =   sr.ReadLine();
//write   the   string   with   the   HTML   break   tag
//Note   here   write   method   writes   to   a   Internal   StringBuilder   object   created   automatically
sw.Write(temp+ " <br> ")   ;
}  
//Return   the   final   processed   text
return   sw.GetStringBuilder().ToString();
}
}
}
 

#10楼 得分:10回复于:2002-10-23 10:14:30
嗯,如果允许HTML的话会很麻烦的,比如说在   <table>   之后的   CrLf   不能换成   <br>   ...
 

#11楼 得分:0回复于:2002-10-23 10:14:57
嗯,如果允许HTML的话会很麻烦的,比如说在   <table>   之后的   CrLf   不能换成   <br>   ...
 

#12楼 得分:20回复于:2002-10-23 10:43:46
写一个类进行格式文本
protected   string   TransStringTool(string   aTransString)
{
    string   returnString   =   " ";
    aTransString   =   aTransString.Replace( "&lt; ", " < ");
    aTransString   =   aTransString.Replace( "&gt; ", "> ");
    aTransString   =   aTransString.Replace( "&nbsp; ", "   ");//空格
    aTransString   =   aTransString.Replace( " <p> ", " <p> ");//回车
    returnString   =   aTransString;
    return   returnString;
}
调用:
LabMemorabiliaContent.Text=TransStringTool(dr[ "cntContent "].ToString());
 

  • xiaomaoxia用户头像
  • xiaomaoxia
  • (一只小蜜蜂啊飞到草丛中啊)
  • 等 级:
#13楼 得分:0回复于:2002-10-24 09:52:22
我用 <pre> 算了
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值