问题一:
以下为我找到网上的一种方法,不过不可行,会提示编绎错误:编译器错误信息: CS0103: 当前上下文中不存在名称“htmlInputTxt”,不知道怎么解决,请高手帮忙
在@ Page页面元素内加以下字段ValidateRequest = "false"禁用ASP.NET请求验证 
使用HtmlEncode方法对输入的字符串进行编码 
使用StringBuilder对象,调用它的Replace方法对字符中的HTML进行替换 
下面的代码给出了这种办法的示例.此页面设置ValidateRequest = "fasle"禁用了ASP.NET请求验证.它的HTML编码为了显示简单的文本格式允许使用<b>和<i>标记.<%@ Page Language="C#" ValidateRequest="false"%><script runat="server">void submitBtn_Click(object sender, EventArgs e)
{
// Encode the string input
StringBuilder sb = new StringBuilder(
HttpUtility.HtmlEncode(htmlInputTxt.Text));
// Selectively allow and <i>
sb.Replace("&lt;b&gt;", "<b>");
sb.Replace("&lt;/b&gt;", "");
sb.Replace("&lt;i&gt;", "<i>");
sb.Replace("&lt;/i&gt;", "");
Response.Write(sb.ToString());
}
</script><html>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="htmlInputTxt" Runat="server" 
TextMode="MultiLine" Width="318px"
Height="168px"></asp:TextBox>
<asp:Button ID="submitBtn" Runat="server" 
Text="Submit" OnClick="submitBtn_Click" />
</div>
</form>
</body>
</html>
问题二:
在用textbox控件输入内容时,我想打个回车就代表<p/>,按一下空格键,就代表是一个“&nbsp;”,并且用lable控件读取时,能正常显示,请高手帮帮忙,谢谢啦

解决方案 »

  1.   

    第一在textbox显示时不要替换,
    在lable显示时使用替换
    写一个替换函数public string HTMLEncode(string textarea)
            {
                if (textarea != "" && textarea != null)
                {
                    textarea = textarea.Replace("&", "&amp;");
                    textarea = textarea.Replace(">", "&gt;");
                    textarea = textarea.Replace("<", "&lt;");
                    textarea = textarea.Replace("\x20", "&nbsp;");
                    textarea = textarea.Replace("\"", "&quot;");
                    textarea = textarea.Replace("\r\r", "</P><P>");
                    textarea = textarea.Replace("\r", "<BR> ");
                    textarea = textarea.Replace("\n\n", "</P><P>");
                    textarea = textarea.Replace("\n", "<BR> ");
                    textarea = textarea.Replace("\0", "&nbsp;");
    //…………………………等等……
                    return textarea;            }
                return string.Empty;
            }在使用lable显示时调用函数HTMLEncode
      

  2.   

    你可以参考我以前写过的一个类似BBCode 的代码:
            //
            public static string BackToHTML(string content,bool Remove)
            {
                string pattern = string.Empty;
                string result = content;
                if (Remove)
                {
                    //this Regex pattern is from Internet, if you want to improve it,
                    //go ahead, I am not good at this.
                    pattern = @"‹/?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?›";
                    result = Regex.Replace(content, pattern, string.Empty, RegexOptions.IgnoreCase);
                }
                else
                {
                    pattern = @"‹/?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?›";
                    MatchCollection mc = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);                for (int i = 0; i < mc.Count; i++)
                    {
                        result = result.Remove(mc[i].Index, 1);
                        result = result.Insert(mc[i].Index, "<");
                        //
                        result = result.Remove(mc[i].Index + mc[i].Value.Length - 1, 1);
                        result = result.Insert(mc[i].Index + mc[i].Value.Length - 1, ">");                }
                    ////result = Regex.Replace(content, pattern, "<", RegexOptions.IgnoreCase);
                    ////
                    ////
                    //pattern = "[>]";
                    //result = Regex.Replace(content, pattern, ">", RegexOptions.IgnoreCase);
                }
                return result;
                //        }
      

  3.   

    public static string ClearHTML(string content, bool RemoveTags)
            {
                string pattern = string.Empty;
                string result = content;
                if (RemoveTags)
                {
                    //this Regex pattern is from Internet, if you want to improve it,
                    //go ahead, I am not good at this.
                    pattern = @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>";
                    result = Regex.Replace(content, pattern, string.Empty, RegexOptions.IgnoreCase);
                }
                else
                {
                    pattern = @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>";
                    MatchCollection mc = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);                for (int i = 0; i < mc.Count; i++)
                    {
                        result = result.Remove(mc[i].Index, 1);
                        result = result.Insert(mc[i].Index, "‹");
                        //
                        result = result.Remove(mc[i].Index + mc[i].Value.Length - 1, 1);
                        result = result.Insert(mc[i].Index + mc[i].Value.Length - 1, "›");                }
                    ////result = Regex.Replace(content, pattern, "<", RegexOptions.IgnoreCase);
                    ////
                    ////
                    //pattern = "[>]";
                    //result = Regex.Replace(content, pattern, ">", RegexOptions.IgnoreCase);
                }
                return result;
                //        }
      

  4.   

            public static string ResolveFaceImages(string inbbcode)
            {
                string[] faces = new string[] { ":D", ":)", ":(", ":o", "8O", ":?", "8)", ":lol:", ":x", ":P", ":oops:", ":cry:", ":evil:", ":twisted:", ":roll:", ":wink:", ":!:", ":?:", ":idea:", ":arrow:" };
                string[] tags = new string[]{"<img src='Images/Icons/icon_biggrin.gif' alt='Very Happy'/>",
                                             "<img src='Images/Icons/icon_smile.gif' alt='Smile'/>",
                                             "<img src='Images/Icons/icon_sad.gif' alt='Sad'/>",
                                             "<img src='Images/Icons/icon_surprised.gif' alt='Surprised'/>",
                                             "<img src='Images/Icons/icon_eek.gif' alt='Shocked'/>",
                                             "<img src='Images/Icons/icon_confused.gif' alt='Confused'/>",
                                             "<img src='Images/Icons/icon_cool.gif' alt='Cool'/>",
                                             "<img src='Images/Icons/icon_lol.gif' alt='Laughing'/>",
                                             "<img src='Images/Icons/icon_mad.gif' alt='Mad'/>",
                                             "<img src='Images/Icons/icon_razz.gif' alt='Razz'/>",
                                             "<img src='Images/Icons/icon_redface.gif' alt='Embarassed'/>",
                                             "<img src='Images/Icons/icon_cry.gif' alt='Crying or very sad'/>",
                                             "<img src='Images/Icons/icon_evil.gif' alt='Evil or Very Mad'/>",
                                             "<img src='Images/Icons/icon_twisted.gif' alt='Twisted Evil'/>",
                                             "<img src='Images/Icons/icon_rolleyes.gif' alt='Rolling Eyes'/>",
                                             "<img src='Images/Icons/icon_wink.gif' alt='Wink'/>",
                                             "<img src='Images/Icons/icon_exclaim.gif' alt='Exclamation'/>",
                                             "<img src='Images/Icons/icon_question.gif' alt='Question'/>",
                                             "<img src='Images/Icons/icon_idea.gif' alt='Idea'/>",
                                             "<img src='Images/Icons/icon_arrow.gif' alt='Arrow'/>"
                                            };
                string result = inbbcode;
                for (int i = faces.Length-1; i >=0; i--)
                {
                    result = result.Replace(faces, tags[i]);
                }
                return result;
            }
            // public static string ResolveBBCodeString(string inbbcode)
            {
                //these inputs can be read from a file(XML file).
                string[] bbtags = new string[] { "", "", "[i]", "
    ", "", "", "", "[code]", "[/code]", "", "", "[br/]", @"\[size=\d\d\]", "[/size]", @"\[color=\w*\]", "[/color]" };
                string[] htmltags = new string[] { "<b>", "</b>", "<i>", "</i>", "<u>", "</u>", "<blockquote><p style='font-style:italic'>", "</p></blockquote>", "<code style='background-color:Grey'>", "</code>", "<url>", "</url>", "<br />", "<span style='font-size:value'>", "</span>", "<span style='color:value'>", "</span>" };
                // It should be bbtags.Length == htmltags.Length;            //true to remove the html tag, false to keep it.
                string bbcodestring = ClearHTML(inbbcode,false);
                //
                
                for (int i = 0; i < bbtags.Length; i++)
                {
                    //int index = bbcodestring.ToLower().IndexOf(bbtags[i]);                MatchCollection mc = Regex.Matches(bbcodestring, bbtags[i]);
                    for (int x = 0; x < mc.Count; x++)
                    {
                        Match mth = mc[x];
                        //Regex.Replace(bbcodestring, bbtags[i], htmltags[i],RegexOptions.Multiline);
                        if (mth.Index >= 0) //found the tag.
                        {
                            string newValue = htmltags[i];
                            string oldValue = bbtags[i];
                            if (mth.Value.Contains("="))
                            {
                                int startIndex = mth.Value.IndexOf('=') + 1;
                                int endIndex = mth.Value.LastIndexOf(']') - startIndex;
                                string value = mth.Value.Substring(startIndex, endIndex);
                                string keyword = mth.Value.Substring(1, startIndex - 2).ToLower();
                                newValue = newValue.Replace("value", value);
                                //right now we only allow size and color of text.
                                if (keyword.Equals("size"))
                                {
                                    oldValue = oldValue.Replace("\\d", string.Empty);
                                    oldValue = oldValue.Replace("\\", string.Empty);
                                    oldValue = oldValue.Insert(oldValue.IndexOf('=') + 1, value);
                                }
                                else if (keyword.Equals("color"))
                                {
                                    oldValue = oldValue.Replace("\\w*", string.Empty);
                                    oldValue = oldValue.Replace("\\", string.Empty);
                                    oldValue = oldValue.Insert(oldValue.IndexOf('=') + 1, value);
                                }
                                else { }                        }
                            bbcodestring = bbcodestring.Replace(oldValue, newValue); //replace the tags with the limited tags.                    }
                    }
                }
                //
                bbcodestring = bbcodestring.Replace("\n", "<br />");
                return ResolveFaceImages(bbcodestring);
                //
                //return bbcodestring;
                //This method should also checked for scripts, links that might hurts our website.
                //which some people call it script injection.
            }
        }
      

  5.   

    genlinux ,Although I can't understand,thank you all the same!
    程序里面没注释,我看得头大啊,具体的调用,请说明一下,我是个新手啊,谢谢各位高手啊!
      

  6.   

    你复制上面的那些方法, 然后调用ResolveBBCodeString(string bbcodeBody);
    //
                string[] bbtags = new string[] { "", "", "", "", "", "", "", "[code]", "[/code]", "", "", "[br/]", @"\[size=\d\d\]", "[/size]", @"\[color=\w*\]", "[/color]" };
                string[] htmltags = new string[] { "<b>", "</b>", "<i>", "</i>", "<u>", "</u>", "<blockquote><p style='font-style:italic'>", "</p></blockquote>", "<code style='background-color:Grey'>", "</code>", "<url>", "</url>", "<br />", "<span style='font-size:value'>", "</span>", "<span style='color:value'>", "</span>" };string bbcodeBody = "1. Hello World\n&nbspGenlinux";
    ResolveBBCodeString(bbcodeBody);
    想对应的输出就是: 1. <b>Hello World</b><br/>&nbsp<i>Gentlinux</i>
    如果你想添加更多的html tag,你可以修改ResolveBBCodeString函数中的string[] bbtags 和string[] htmltags
    不过它们要相对应的.
      

  7.   

    如果你输入ResolveBBCodeString("<a href="www.viruslink.com">Click Me</a>");
    那么这里的"<"和">"将会被替换成特殊符号:"›" 和"‹", 那么输出就成为:
    ‹a href="www.viruslink.com"›Click Me‹/a›"