-3

我有一个文章表,当我显示我想在转发器的标签中显示的文章时,该表包含一个名为“dateofSubmitArticle”的字段,这篇文章在转发器的数据库数据源中提交的时间是多少

 var n = (from a in DataContext.Context.Articles
                 join u in DataContext.Context.Users
                 on a.UserID equals u.UserID
                 orderby a.ArticleID descending
                 where a.IsConfirmByAdmin == true
                 select new { 
                 ArticleID=a.ArticleID,
                 ArticleTitle=a.ArticleTitle,
                 ArticleSummery=a.ArticleSummery,
                 SubmitDate=a.SubmitDate,
                 Username=u.Username,
                 ArticleImageName=a.ArticleImageName
                 }).Take(15);
        rptArticleTitle.DataSource = n;
        rptArticleTitle.DataBind();
4

1 回答 1

0

您可以使用 Timespan 类。为了实现您的目标,在转发器中放置一个 asp 标签和隐藏字段,将 SubmitDate 绑定到该隐藏字段。

HTML:

<asp:Label runat="server" id="Submitted" />
<asp:Hidden runat="server" id="val" Value='<%#Eval("SubmitDate")%>'/>

现在在中继器 ItemDataBound 中找到标签并隐藏并使用下面的代码。

void ItemDataBound()
{
  if(e.Item.ItemType == DataItemType || e.Item.ItemType ==AlternatingItemType)
  {
      var lbl = e.Item.FindControl(lable_id) as Label;
      var hid = e.Item.FindControl(hidden_id) as Hidden;

      if(lbl !=null and hid !=null)
      {
          var val = DateTime.Parse(hid.Value);
         // Get the TimeSpan of the difference. 
         TimeSpan elapsed = now.Subtract(val); 
         lbl.Text = string.Format("{0} days ago", elapsed.Days);
      }

  }
}
于 2013-09-05T18:51:45.400 回答