【问题标题】:C# Webforms - Add <strong> around text portion if a condition is metC# Webforms - 如果满足条件,则在文本部分周围添加 <strong>
【发布时间】:2015-11-19 15:40:19
【问题描述】:

我正在向 C# Webforms (Bootstrap) 站点添加简单的博客功能,并尝试在右侧列中创建站点存档。存档显示过去 6 个月的列表,无论该月是否发表了任何文章。每个月(列表中的项目)都附有一个徽章,其中显示了该月发布的帖子数量。如果没有帖子,则徽章会折叠(Bootstrap)。另外,当前选中的月份是加粗的,这就是我的问题所在。

我想有序地迭代过去6个月,计算给定月份的文章数量以及是否是选定的月份。

这是存档当前的样子(测试颜色:)):

如您所见,9 月已突出显示,因为它是 URL (...BlogArchive?year=2015&amp;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>

然而,这不太理想,因为我重复了两次相同的&lt;a&gt; 标签语句——唯一的区别是一个应用了&lt;strong&gt; 标签(和结束标签)而另一个没有。此外,我将添加功能,以便所有没有帖子的月份都不会包含链接(因为没有什么可看的 -> 即 6 月 - 8 月)。目前,这将涉及在上面的两个&lt;a&gt; 语句中添加相同的if 语句,这似乎又是毫无意义的重复,特别是如果我将来需要更改它。

有没有办法简化上述语句,这样我就不会重复相同的&lt;a&gt; 语句?如果列表月份等于所选月份,我正在尝试考虑一种仅应用 &lt;strong&gt; 标签的方法,但不知道如何放入结束标签 (&lt;/strong&gt;),因为那里无论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


    【解决方案1】:

    你可以定义一个类女巫集

    font-weight: bold;
    

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

    您的代码将如下所示:

    string stylingclass = String.Empty;
    if(condition) {stylingclass = "YourCssClassHere";}
    
    ....
    <a href="<%=archiveURL %>>" class="list-group-item <%=stylingclass%>"><%=monthCounter.ToString("MMMM yyyy") %>
    ....
    

    【讨论】:

    • 谢谢。我曾经想过这一点,但由于我最终希望几个月没有任何帖子仅显示为文本而不是链接这一事实而放弃了它。但是,我并没有真正考虑太多,这促使我再次考虑。我会试试看它是否有效!
    • 我刚刚收到了对我向客户提出的问题的回复,发现我现在需要在一个月内有帖子时使用&lt;a&gt; 标签,当有帖子时使用&lt;span&gt;没有帖子(这可以防止用户访问空白页面)。还是谢谢!
    【解决方案2】:

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

    <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 来呈现链接。此外,标签用于创建带有 CssClass 的跨度,以便您可以设置样式。

    要提供数据,您需要一个与此类似的类:

    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; }
    }
    

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

    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 更改为没有帖子的月份。第二个视图不包含超链接控件,因此不呈现a 标记。

    最后,您需要在 CSS 中定义类以将文本格式化为粗体。

    【讨论】:

    • 这看起来很有趣!但是,我刚刚收到确认,如果一个月内没有帖子,则用户应该无法访问该月。因此,如果当月没有帖子,我需要输出&lt;span&gt; 而不是&lt;a&gt;(否则用户将被带到一个空白页面)。不管怎么说,还是要谢谢你!我一定会记住这一点以备将来使用!
    • @KendallRoth 我已经更新了我的答案,因此如果一个月内没有帖子,您也可以将 URL 留空。这看起来肯定是一个很大的变化,但它会得到回报。
    • 谢谢。那会起作用,但这是否仍然允许用户单击链接,或者至少认为它是一个链接(并且在单击什么都不做时感到沮丧)?另外,我认为留下一个空的href="" 不会通过我们现有的一些检查……标准相当严格。
    • @KendallRoth 在一个小测试中,如果 href 不仅为空而且被省略,我的浏览器没有显示链接。当然,如果您的检查如此严格,您将需要一些 if 语句。
    • @KendallRoth 我已将 MultiView 添加到 ItemTemplate。超链接仅在存在 ArchiveUrl 时使用。
    猜你喜欢
    • 2016-11-06
    • 2020-05-10
    • 2015-07-09
    • 2012-05-12
    • 2020-09-11
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    相关资源
    最近更新 更多