我的博客网站开发4——博客首页功能实现之博文摘要

      在很多的网站都可看见对文章摘要的显示,这种用户体验使读者在未完全打开文章全部内容时,就可对文章有一个大概的了解,需进一步了解文章内容时再进行点开阅读。     

      实现博客主要的博文显示,其中包括博文的标题、博文的摘要(通过算法实现)、博文的相关信息的显示。由于是使用GridView控件,只需要对控件进行数据控件的绑定即可。其中最主要的还是对博文摘要的相关处理。由于后面还会涉及到博文编辑器的实现,博文在发表的时候,很多时候都要进行段落和字体的处理,由于我在保存博文时是采用直接将其放进数据库的字段中,自然就会要保存相关的html标签代码。问题就出现在我要对摘要进行显示时,自然就希望摘要不需要那些html标签代码,这就涉及到去除hrml代码的问题了。

通过以下方法即可去除htnl标签代码,C#代码:

 1 //摘要中去除html标记
 2         public static string RemoveHTML(string Htmlstring)
 3         {
 4             //删除脚本   
 5             Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
 6             //删除HTML   
 7             Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
 8             Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
 9             Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
10             Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
11 
12             Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
13             Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
14             Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
15             Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
16             Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
17             Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
18             Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
19             Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
20             Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
21             Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
22 
23             Htmlstring.Replace("<", "");
24             Htmlstring.Replace(">", "");
25             Htmlstring.Replace("\r\n", "");
26             Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
27 
28             return Htmlstring;
29         }

还存在一个简单的问题,就是既然是摘要,必然就会截取前面一部分文字,我的希望获取前面250字,方法如下:

str.Substring(0, str.Length > 250 ? 250 : str.Length);//截取博文前250字的摘要

htnl代码:

 1  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
 2         <Columns>
 3             <asp:TemplateField>
 4                 <ItemTemplate>
 5                     <div>
 6                     <div class="articletitle" style=" background:rgb(216, 223, 234); text-align:left; width:660px;">
 7                     <a href="../BlogContent.aspx/<%=getMasterId() %>?id=<%# DataBinder.Eval(Container.DataItem, "essayId") %>"><%# DataBinder.Eval(Container.DataItem, "title") %></a>
 8                     </div>
 9                     <div class="articlecontent" style=" text-align:left; width:660px;">
10                     <span>摘要:</span>
11                     <%# DataBinder.Eval(Container.DataItem, "essayContent")%>
12                     <span><a href="../BlogContent.aspx/<%=getMasterId() %>?id=<%# DataBinder.Eval(Container.DataItem, "essayId") %>">阅读全文</a></span>
13                     </div>
14                     <div class="extrainfo" style=" text-align:right;">
15                     <span></span><span>发表于:<%# DataBinder.Eval(Container.DataItem, "essaypostTime")%></span><span>评论()</span><span>阅读(<%# DataBinder.Eval(Container.DataItem, "essayClickCount")%>)</span>
16                     </div>
17                     </div>
18                 </ItemTemplate>
19             </asp:TemplateField>
20         </Columns>
21     </asp:GridView>

后台CS的C#代码:

 1         public void BindingGDV(DataTable dt)
 2         {
 3             for (int i = 0; i < dt.Rows.Count; i++)
 4             {
 5                 string str = dt.Rows[i]["essayContent"].ToString();
 6                 str = str.Substring(0, str.Length > 250 ? 250 : str.Length);//截取博文前250字的摘要
 7                 dt.Rows[i]["essayContent"] = RemoveHTML(str);
 8             }
 9             GridView1.DataSource = dt;
10             GridView1.DataKeyNames = new string[] { "essayId" };
11             GridView1.DataBind();
12         }

至此博客的首页功能基本完成

 

posted @ 2012-05-06 20:36  Ghost Soar  阅读(539)  评论(0编辑  收藏  举报