2011-05-12 70 views
0

场景:。我在xml文件中有30个提示,现在我想按照天数在主页上显示它。在asp.net中按天数在主页上显示提示

例如,今天是12号,然后提示号码12将显示在主页上,之后13号等等。经过30个提示它将从尖1

我曾尝试下面的代码

public class TipReader 
{ 
    private const string TIP_XML_PATH = @"C:\temp\tips.xml"; 
    public static int GetTipNumber() 
    { 
     int tipNo = 1; 
     if (DateTime.Now.Day >0) 
     { 
      if (DateTime.Now.Day == 31) 
      { 
       tipNo = 1; 
      } 
      else if (DateTime.Now.Day == 28) 
      { 
       tipNo = DateTime.Now.Day + 1; 
      } 
      else 
      { 
       tipNo = DateTime.Now.Day; 
      } 
     } 
     return tipNo; 
    } 
    public IEnumerable<Tip> GetTipByID(string ID) 
    { 
     var result = from tip in XDocument.Load(TIP_XML_PATH).Descendants("Tip") 
        where (string)tip.Element("ID")==ID 
        select new Tip 
        { 
         ImageUrl = (string)tip.Element("ImageUrl"), 
         Title = (string)tip.Element("Title").Value, 
         Desciption = (string)tip.Element("Description"), 
        }; 
     return result; 
    } 
} 

现在,我就GetTipNumber面临的问题开始。如何处理日期大于30且小于30. 如果您有更好的解决方案,请给我建议。 在此先感谢

+0

你有什么问题? – 2011-05-12 11:43:44

+0

Abdul:没问题,有一些混淆:) – 2011-05-12 11:46:57

回答

3

什么是用这种方式做

public static int GetTipNumber() 
{ 
    int tipNo = 1; 
    if (DateTime.Now.Day > 0 && DateTime.Now.Day <= 30) 
    { 
     tipNo = DateTime.Now.Day; 
    } 
    else 
    { 
     tipNo = 1; // 
    }     

    return tipNo; 
} 
+0

谢谢你的答案。我会检查你的代码,并让你知道很快 – 2011-05-12 11:47:40

0

另一种选择的危害是,在每次显示随机提示您查看页面:

public static int GetTipNumber() 
{ 
    Random rand = new Random(); 
    return rand.Next(1, 30); 
} 
1
int tipNo = DateTime.Now.Day > 30 ? 1 : DateTime.Now.Day;