2011-11-05 100 views
0

我希望顯示一個想法列表,而不是僅顯示每天改變的1個想法。有沒有辦法編輯我的代碼,以秒數而不是新的一天來顯示更改?顯示使用c的想法列表#

如何編輯下面的代碼?

using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 

namespace TOTD.TOTD 
{ 
    public partial class TOTDUserControl : UserControl 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      SPWeb oWebsite = SPContext.Current.Web; 
      SPList oList = oWebsite.Lists["QOTD"]; 
      SPListItemCollection collItem = oList.GetItems("Thought", "AuthorImage", "AuthorName","Head"); 

      Random random = new Random(); 
      int RndItem = random.Next(1, collItem.Count + 1); 
      int LastDay = 0; 
      int TOTD = 0; 
      int CurrentDay = DateTime.Now.DayOfYear; 

      try 
      { 

       LastDay = int.Parse(Application["LastDay"].ToString()); 
       TOTD = int.Parse(Application["TOTD"].ToString()); 


       if (LastDay != CurrentDay) 
       { 

        Application["LastDay"] = CurrentDay; 
        Application["TOTD"] = RndItem; 
        SPListItem oItem = collItem[RndItem - 1]; 
        this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode 
         (oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' ')); 
        this.lblTOTD.Text = oItem["Thought"].ToString(); 
        this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString()); 
        this.lblNext.Text = SPEncode.HtmlEncode(oItem["Head"].ToString()); 
       } 

       else 
       { 
        SPListItem oItem = collItem[TOTD - 1]; 
        this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode 
         (oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' ')); 
        this.lblTOTD.Text = oItem["Thought"].ToString(); 
        this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString()); 
        this.lblNext.Text = SPEncode.HtmlEncode(oItem["Head"].ToString()); 
       } 

      } 
      catch 
      { 
       Application["LastDay"] = CurrentDay; 
       Application["TOTD"] = RndItem; 
       SPListItem oItem = collItem[RndItem - 1]; 
       this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode 
        (oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' ')); 
       this.lblTOTD.Text = oItem["Thought"].ToString(); 
       this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString()); 
       this.lblNext.Text = SPEncode.HtmlEncode(oItem["Head"].ToString()); 
      } 
     } 
    } 
} 
+0

也許insted的LASTDAY = INT的....只是有LastTime = INT ....如果當前時間是更大的某些秒數顯示你想要的 –

回答

0

只需保存相關DateTime和使用Subtract方法:

if (DateTime.Now.Subtract(givenDate).TotalSeconds >= 1) 
{ 
    Application["LastDay"] = DateTime.Now; 
// do stuff. 
}...