取决于不同的解决机制:

解决方案1:

在数据库中存入标签的HTML转换符,按照原样输出。

在存入数据库之前,加上server.htmlEncode(txtboxName.text);

解决方案2:

在数据库中不存入HTML标签的有关信息,过滤HTML标签,只显示文字。

存入数据库之前加上过滤函数过滤便可:checkStr(txtboxName.text)或者StripHTML(txtboxName.text)或者NoHTML(txtboxName.text);(提供三个过滤函数,任选一个,添加命名空间:using System.Text.RegularExpressions;)

以下代码均调试通过:

  

/// <summary>
/// HTML过滤方法一
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public string checkStr(string
        {
Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
""); //过滤<script></script>标记
""); //过滤href=javascript: (<A>) 属性
" _disibledevent="); //过滤其它控件的on...事件
""); //过滤iframe
""); //过滤frameset
""); //过滤frameset
""); //过滤frameset
""); //过滤frameset
"");
" ", "");
"</strong>", "");
"<strong>", "");
return
        }
 
#region 过滤掉html代码
 
 
///   <summary>
///   方法二:去除HTML标记
///   </summary>
///   <param   name="StripHtml">包括HTML的源码  </param>
///   <returns>已经去除后的文字</returns>
 
public static string StripHTML(string
        {
string[] aryReg ={ 
@"<script[^>]*?>.*?</script>", 
 
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", 
@"([\r\n])[\s]+", 
@"&(quot|#34);", 
@"&(amp|#38);", 
@"&(lt|#60);", 
@"&(gt|#62);", 
@"&(nbsp|#160);", 
@"&(iexcl|#161);", 
@"&(cent|#162);", 
@"&(pound|#163);", 
@"&(copy|#169);", 
@"&#(\d+);", 
@"-->", 
@"<!--.*\n"
                               };
 
string[] aryRep = { 
"", 
"", 
"", 
"\"", 
"&", 
"<", 
">", 
" ", 
"\xa1",//chr(161), 
"\xa2",//chr(162), 
"\xa3",//chr(163), 
"\xa9",//chr(169), 
"", 
"\r\n", 
""
                               };
 
string
string
for (int
            {
Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
"<", "");
">", "");
"\r\n", "");
return
        }
        #endregion
 
 
 
 
 
///   <summary>
///   方法三:去除HTML标记
///   </summary>
///   <param   name="NoHTML">包括HTML的源码  </param>
///   <returns>已经去除后的文字</returns>
public static string NoHTML(string
        {
//删除脚本
Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
RegexOptions.IgnoreCase);
//删除HTML
Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
RegexOptions.IgnoreCase);
Regex.Replace(Htmlstring, @"&#(\d+);", "",
RegexOptions.IgnoreCase);
"<", "");
">", "");
"\r\n", "");
HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return
        }