我做了一个论坛,其中论坛的帖子显示是用TextBox,<asp:textbox id="postContentText" cssclass="content" Runat="server" TextMode="MultiLine" Text="<%# DataBinder.Eval(Container.DataItem, "TopicContent") %>" ></asp:textbox>我想把用户发的帖子中的如: http://www.sina.com/postNew.asp?ID=122  等自动加上超链接(现在很多论坛都有这个功能),请问该如何办?

解决方案 »

  1.   

    my= new Regex(@"(\[URL\])(http:\/\/.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""$2"" TARGET=_blank>$2</A>"); my=new Regex(@"(\[URL\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""http://$2"" TARGET=_blank>$2</A>"); my=new Regex(@"(\[URL=(http:\/\/.[^\[]*)\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""$2"" TARGET=_blank>$3</A>");
        
    my=new Regex(@"(\[URL=(.[^\[]*)\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""http://$2"" TARGET=_blank>$3</A>"); my=new Regex(@"(\[EMAIL\])(\S+\@.[^\[]*)(\[\/EMAIL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""mailto:$2"">$2</A>"); my=new Regex(@"(\[EMAIL=(\S+\@.[^\[]*)\])(.[^\[]*)(\[\/EMAIL\])",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<A HREF=""mailto:$2"" TARGET=_blank>$3</A>"); my=new Regex(@"^(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<a target=_blank href=$1>$1</a>"); my=new Regex(@"(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<a target=_blank href=$1>$1</a>");
      

  2.   

    试过了,在 textbox 中还是显示不出超链接。
      

  3.   


    我是这样用的, Content是原来的帖子内容,newContent是转换后的帖子,发现还是没有变化:
    在.aspx中:<asp:textbox id="postContentText" cssclass="content" Runat="server" TextMode="MultiLine" Text="<%# ProcessLink(DataBinder.Eval(Container.DataItem, WebTutorial.Common.Data.ForumPostData.CONTENT_FIELD)) %>" ForeColor="#636060"></asp:textbox>在后台代码中: public string ProcessLink(object Content)
    {
    string strContent = Content.ToString();
    Regex my= new Regex(@"(\[URL\])(http:\/\/.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""$2"" TARGET=_blank>$2</A>"); my=new Regex(@"(\[URL\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""http://$2"" TARGET=_blank>$2</A>"); my=new Regex(@"(\[URL=(http:\/\/.[^\[]*)\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""$2"" TARGET=_blank>$3</A>");
        
    my=new Regex(@"(\[URL=(.[^\[]*)\])(.[^\[]*)(\[\/URL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""http://$2"" TARGET=_blank>$3</A>"); my=new Regex(@"(\[EMAIL\])(\S+\@.[^\[]*)(\[\/EMAIL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""mailto:$2"">$2</A>"); my=new Regex(@"(\[EMAIL=(\S+\@.[^\[]*)\])(.[^\[]*)(\[\/EMAIL\])",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<A HREF=""mailto:$2"" TARGET=_blank>$3</A>"); my=new Regex(@"^(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<a target=_blank href=$1>$1</a>"); my=new Regex(@"(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$",RegexOptions.IgnoreCase);
    strContent=my.Replace(strContent,@"<a target=_blank href=$1>$1</a>");
    string newContent = strContent;
    return newContent;
    }
      

  4.   

    超链接怎么能嵌在TextArea(TextBox)里面,这是HTML语法的问题,不要用TextBox来显示帖子内容。只用于修改……
      

  5.   

    用Lable也不行啊。<asp:Label id="postContentText" Runat="server" TextMode="MultiLine" Text="<%# ProcessLink(DataBinder.Eval(Container.DataItem, WebTutorial.Common.Data.ForumPostData.CONTENT_FIELD)) %>" ForeColor="#636060"></asp:Label>
      

  6.   

    关键是我在public string ProcessLink(object Content)中设置断点,发现 http://www.sina.com 并没有变成 <a href = "http://www.sina.com">http://www.sina.com </a>,也就是说xiahouwen(武眉博<活靶子.NET>) 的转换还有问题。