我正致力于向 C# Webforms (Bootstrap) 网站添加简单的博客功能,并尝试在右侧栏中创建网站存档。存档显示过去 6 个月的列表,无论该月是否发表了任何文章。每个月(列表中的项目)都有一个徽章,其中显示了该月发布的帖子数量。如果没有帖子,徽章就会折叠 (Bootstrap)。另外,当前选择的月份是加粗的,这就是我的问题所在。
我想有序地迭代过去 6 个月,计算给定月份的文章数量以及是否是所选月份。
这是存档当前的样子(测试颜色 :)):
如您所见,九月已突出显示,因为它是由 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 月)。目前,这将涉及将相同的 if 语句添加到上面的两个 <a> 语句,这似乎又是毫无意义的重复,特别是如果我将来需要更改它。
有没有办法简化上面的语句,这样我就不会重复相同的 <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# 输出到页面的最佳方式吗?
请您参考如下方法:
你可以定义一个类女巫集
font-weight: bold;
根据您的情况加类或不加类
你的代码看起来像这样:
string stylingclass = String.Empty;
if(condition) {stylingclass = "YourCssClassHere";}
....
<a href="<%=archiveURL %>>" class="list-group-item <%=stylingclass%>"><%=monthCounter.ToString("MMMM yyyy") %>
....
