C# Webforms - 如果满足条件,则在文本部分周围添加 <strong>

C# Webforms - Add <strong> around text portion if a condition is met

我正在为 C# Webforms (Bootstrap) 站点添加简单的博客功能,并尝试在右侧栏中创建站点存档。存档显示过去 6 个月的列表,无论该月是否发表了任何文章。每个月(列表中的项目)都有一个徽章,其中显示了该月发布的 post 数量。如果没有 post,则徽章会折叠 (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> 标记(和结束标记)并且另一个没有。此外,我将添加功能,以便所有没有 posts 的月份都不会包含 links(因为没有什么可看的 -> 即 6 月至 8 月)。目前,这将涉及将相同的 if 语句添加到上面的两个 <a> 语句,这似乎又是毫无意义的重复,特别是如果我将来需要更改它。

有没有办法简化上面的语句,这样我就不会重复相同的 <a> 语句?如果列表月份等于所选月份,我正在尝试考虑一种仅应用 <strong> 标记的方法,但不知道适合结束标记的方法 (</strong> ),因为无论 if 语句的结果如何(即徽章和 post 计数),都需要存在文本。

谢谢!


更新

我现在收到回复说我需要输出一个 link 只有当有博客 post 一个月(否则只是文本)。这是为了防止用户访问空白页面。

我是这样解决的:

<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# 输出到页面的最佳方式吗?

你可以定义一个class集合

font-weight: bold;

根据您的情况添加 class 或不添加 class

您的代码将如下所示:

string stylingclass = String.Empty;
if(condition) {stylingclass = "YourCssClassHere";}

....
<a href="<%=archiveURL %>>" class="list-group-item <%=stylingclass%>"><%=monthCounter.ToString("MMMM yyyy") %>
....

您可以将代码更改为 Repeater,从而将布局与逻辑分开,而不是直接输出文本并用 strong 标记围绕它。您的 Repeater 看起来类似于:

<asp:Repeater ID="rptMonths" runat="server">
    <HeaderTemplate>
        <div class="list-group blog-archive">
    </HeaderTemplate>
    <ItemTemplate>
        <asp:MultiView runat="server" ActiveViewIndex='<%# Eval("ViewIndex") %>'>
            <asp:View runat="server">
                <asp:HyperLink runat="server" NavigateUrl='<%# Eval("ArchiveUrl") %>'
                      CssClass="list-group-item">
                    <asp:Label runat="server" Text='<%# Eval("MonthName") %>' 
                          CssClass='<%# Eval("MonthClass") %>' />
                    <span class="badge"><%# Eval("NumberOfPosts") %></span>
                </asp:HyperLink>
            </asp:View>
            <asp:View runat="server">
                <asp:Label runat="server" Text='<%# Eval("MonthName") %>'
                    CssClass='<%# Eval("MonthClass") %>' />
                <span class="badge"><%# Eval("NumberOfPosts") %></span>
            </asp:View>
        </asp:MultiView>
    </ItemTemplate>
    <FooterTemplate>
        </div>
    </FooterTemplate>
</asp:Repeater>

请注意,仅当设置了 URL 时才使用 MultiView 呈现 link。此外,标签用于创建带有 CssClass 的跨度,以便您可以设置样式。

要提供数据,您需要 class 类似于此:

public class MonthSummary
{
    public DateTime Month { get; set; }
    public string MonthName { get { return Month.ToString("MMMM"); } }
    public string MonthClass { get; set; }
    public string ArchiveUrl { get; set; }
    public int NumberOfPosts { get; set; }
    public int ViewIndex { get; set; }
}

加载页面时,您会 assemble 月份数据,例如在页面加载中:

public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var lst = new List<MonthSummary>();
        for (var m = DateTime.Today.AddMonths(-5); m <= DateTime.Today; m = m.AddMonths(1))
        {
            var summary = new MonthSummary();
            summary.Month = m;
            if (int.Parse(SelectedYear) == m.Year && int.Parse(SelectedMonth) == m.Month)
                summary.MonthClass = "HighlightedMonth";
            else
                summary.MonthClass = string.Empty;
            summary.NumberOfPosts = 0; // get the number of posts in an appropriate way
            if (summary.NumberOfPosts > 0)
            {
                summary.ArchiveUrl = "~/BlogArchive?year=" + m.Year.ToString() + "&month=" + m.Month.ToString();
                summary.ViewIndex = 0;
            }
            else
            {
                summary.ArchiveUrl = string.Empty;
                summary.ViewIndex = 1;
            }
            lst.Add(summary);
        }
        rptMonths.DataSource = lst;
        rptMonths.DataBind();
    }

}

组装数据时,更改没有帖子的月份的 ViewIndex。第二个视图不包含 Hyperlink 控件,因此不呈现 a 标记。

最后,您需要在 CSS 中定义 class 以将文本设置为粗体。