【发布时间】:2015-11-19 15:40:19
【问题描述】:
我正在向 C# Webforms (Bootstrap) 站点添加简单的博客功能,并尝试在右侧列中创建站点存档。存档显示过去 6 个月的列表,无论该月是否发表了任何文章。每个月(列表中的项目)都附有一个徽章,其中显示了该月发布的帖子数量。如果没有帖子,则徽章会折叠(Bootstrap)。另外,当前选中的月份是加粗的,这就是我的问题所在。
我想有序地迭代过去6个月,计算给定月份的文章数量以及是否是选定的月份。
这是存档当前的样子(测试颜色:)):
如您所见,9 月已突出显示,因为它是 URL (...BlogArchive?year=2015&month=September) 选择的月份。目前,这是我突出显示当前月份的代码:
<div class="list-group blog-archive">
<% DateTime monthCounter = DateTime.Today.AddMonths(-6);
for (int i = 0; i < 6; i++)
{
monthCounter = monthCounter.AddMonths(1);
//Get the number of posts in the current month list-item
string numberOfPostsInMonth = GetNumberOfPostsInMonth(monthCounter.ToString("MMMM"), monthCounter.ToString("yyyy"));
string archiveURL = "";
if (numberOfPostsInMonth != "")
{
//Get the URL to pass to the Blog Archive link 'href'
archiveURL = "BlogArchive?year=" + monthCounter.ToString("yyyy") + "&month=" + monthCounter.ToString("MMMM");
}
else
{
archiveURL = "BlogArchive";
}
if (monthCounter.ToString("yyyy") == SelectedYear && monthCounter.ToString("MMMM") == SelectedMonth)
{
%>
<a href="<%=archiveURL %>" class="list-group-item"><strong><%=monthCounter.ToString("MMMM yyyy") %></strong>
<span class="badge"><%=numberOfPostsInMonth %></span>
</a>
<%
}
else
{
%>
<a href="<%=archiveURL %>" class="list-group-item"><%=monthCounter.ToString("MMMM yyyy") %>
<span class="badge"><%=numberOfPostsInMonth %></span>
</a>
<%
}
} %>
</div>
然而,这不太理想,因为我重复了两次相同的<a> 标签语句——唯一的区别是一个应用了<strong> 标签(和结束标签)而另一个没有。此外,我将添加功能,以便所有没有帖子的月份都不会包含链接(因为没有什么可看的 -> 即 6 月 - 8 月)。目前,这将涉及在上面的两个<a> 语句中添加相同的if 语句,这似乎又是毫无意义的重复,特别是如果我将来需要更改它。
有没有办法简化上述语句,这样我就不会重复相同的<a> 语句?如果列表月份等于所选月份,我正在尝试考虑一种仅应用 <strong> 标签的方法,但不知道如何放入结束标签 (</strong>),因为那里无论if 语句的结果如何(即徽章和帖子计数),两者之间的文本都需要存在。
谢谢!
更新
我现在收到了一条回复,说我需要在一个月内有博客文章时才需要输出链接(否则只需要文本)。这是为了防止用户访问空白页面。
我设法以这种方式解决了它:
<div class="list-group blog-archive">
<% DateTime monthCounter = DateTime.Today.AddMonths(-6);
for (int i = 0; i < 6; i++)
{
monthCounter = monthCounter.AddMonths(1);
string monthHTML = "";
string monthText = monthCounter.ToString("MMMM yyyy");
string monthURL = "";
string badgeHTML = "";
string numberOfPostsInMonth = GetNumberOfPostsInMonth(monthCounter.ToString("MMMM"), monthCounter.ToString("yyyy"));
//Bold the selected month list-item
if (monthCounter.ToString("yyyy") == SelectedYear && monthCounter.ToString("MMMM") == SelectedMonth)
{
monthText = "<strong>" + monthText + "</strong>";
}
//If there are posts in the month, print the month as a link.
// Otherwise, if there are no posts in the month, print the month as a '<span>' rather than a link
if (numberOfPostsInMonth != "")
{
monthURL = "BlogArchive?year=" + monthCounter.ToString("yyyy") + "&month=" + monthCounter.ToString("MMMM");
badgeHTML = "<span class=\"badge\">" + numberOfPostsInMonth + "</span>";
monthHTML = "<a href=\"" + monthURL + "\" class=\"list-group-item\">" + monthText + badgeHTML + "</a>";
}
else
{
monthHTML = "<span class=\"list-group-item\">" + monthText + badgeHTML + "</span>";
}
//Write the month list item to the page
Response.Write(monthHTML);
}
%>
</div>
此时我想知道这是否是最好的方法?尤其是 Response.Write(),这实际上是从 C# 输出到页面的最佳方式吗?
【问题讨论】:
标签: c# asp.net twitter-bootstrap webforms