ASP.NET(datalist) 实现新闻后加标记

效果:datalist显示所有新闻的列表,一个星期以内的新闻后加new字样

前台代码

<div>
                                        <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="450px"
                                            Style="position: relative; top: 9px; float: left;" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
                                            <ItemTemplate>
                                                <table style="width: 100%; color: #0C2774; font-size: 13px !important; font-size: 12px;
                                                    margin-right: 10px;">
                                                    <tr>
                                                        <td align="left">
                                                            <div style="margin-left: 12px;">
                                                                <img src="../image/newsLogimg.png" style="margin-right: 8px;" />
                                                                <a class="thumb" href="###" οnclick='window.open("messageDetails.aspx?name=<%#DataBinder.Eval(Container.DataItem,"Title")%>","_blank","width=740,height=690,scrollbars=1,resizable=1,left=20,top=20")'
                                                                    style="cursor: pointer; text-decoration: none; color: #0C2774; font-size: 12px;">
                                                                    <asp:Label ID="Label2" runat="server" Style="color: #0C2774;" Text='<%# Eval("Title") %>'></asp:Label>
                                                                </a>
                                                                <asp:Label id="newImage" Text="new" BackColor= "Red" runat="server" style="margin-right: 8px;" />
                                                            </div>
                                                        </td>
                                                        <td align="right" style="width: 50px;">
                                                            <asp:Label ID="Label1" runat="server" Style="color: #868277;" Text='<%# Eval("Time") %>'></asp:Label></td>
                                                    </tr>
                                                </table>
                                            </ItemTemplate>
                                        </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:StitpDatabaseConnectionString %>"
                                            SelectCommand="SELECT top 8 [Title], convert(varchar(10),[Time],102) as [Time] FROM [Message]">
                                        </asp:SqlDataSource>
                                    </div>

 

后台代码

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        foreach (DataListItem item in DataList1.Items)
        {
            Label newImage = (Label)item.FindControl("newImage");
            Label TimeLabel = (Label)item.FindControl("Label1");
            DateTime newsTime = Convert.ToDateTime(TimeLabel.Text.ToString());
            TimeSpan newsTimeSpan = new TimeSpan(newsTime.Ticks);
            TimeSpan datetimeNow = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts = datetimeNow.Subtract(newsTimeSpan);
            int days = ts.Days;
            if (days <= 7)//一个星期以内提示
            {
                newImage.Visible = true;
            }
            else
            {
                newImage.Visible = false;
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值