日期:2014-05-18  浏览次数:20387 次

如何把html代码保存到SQL数据库?
现在我在一个文本框中输入了html源文件,但是发现去保存不到数据库,
就是这句话,我想保存到数据库。结果发现保存后为空
string     img= " <img       src= ' "+fileFolder+ "\\ "+filename+ " '       onload= 'javascript:if(this.width> 300){this.style.width=300;} '/> ";

请问如何才能将这个   img字符串保存到数据库中啊?谢谢!


------解决方案--------------------
第一步:
.aspx的文件如果提交Html代码时页面会报错,因为默认的安全级别不是让你提交html代码的
在页面的最上面的 <%@ %> 里加如下代码
ValidateRequest= "false "

第二步:
如果你是想像CSDN这样输入 <table> </table> 就显示 <table> </table> 而不是显示一个表格的话,那就把危险字符替换掉,我这里有一个方法是我自己写的,写的不好请大家不要笑我

public static string CheckTextDanger(string checkText)

{
string newText = checkText.Trim(); //去掉头尾空格
newText = newText.Replace( "\n ", " <br> ");
//textBox里的换行是用\n来表示的,如果要在HTML里显示换行要用 <br>
newText = newText.Replace( " < ", "&lt "); //置换 <
newText = newText.Replace( "> ", "&gt "); //置换 >
newText = newText.Replace( " ' ", " ' ' ");
//如果是用存储过程存储数据,这行不用加,如果你用的是SQL语句来存数据,这行要加上,功能为置换 ‘
return newText;
}


如果你从数据库返回的值要用到TextBox
那还得有个回调的方法,因为TextBox会把Html代码原封不动的列出来
public static string CheckTextOut(string checkText)

{
string newText = checkText.Trim(); //去掉头尾空格
newText = newText.Replace( " <br> ", "\n ");
newText = newText.Replace( "&lt ", " < ");
newText = newText.Replace( "&gt ", "> ");
return newText;
}