代码隐藏在日历背后的天数

时间:2013-05-07 12:49:34

标签: c# asp.net

//DaysinLastMonth gives me the Days in April
//DaysinCurrentMonth gives me the Days in May



     public void RunCalender(int startOfCalender, int DaysinLastMonth,int 
                                                                DaysInCurrentMonth)
        {
            int value = 0, startOfCalender1 = 0;
            for (int i = 0; i < 6; i++)
            {
                HtmlTableRow tr = new HtmlTableRow();

                if (value == 0)
                   // StartOfCalender is the point where the Calender starts 
                   //from in the month display
                    startOfCalender1 = startOfCalender;
                else
                    startOfCalender1 = value;


                for (int j = 0; j < 7; j++)
                {
                    HtmlTableCell tc = new HtmlTableCell();
                    tc.BgColor = "#FAAFBE";
                    // tc.ColSpan = 1;
                    //tc.Attributes.Add("class", "rowA");
                    HtmlAnchor a = new HtmlAnchor();
                    a.HRef = "http://localhost:51955/Calender.aspx";

                    a.InnerHtml = startOfCalender1.ToString();

                    startOfCalender1 += 1;
                    if (startOfCalender1 == DaysinLastMonth + 1)
                    {
                        startOfCalender1 = 1;
                    }
                    value = startOfCalender1;
                    // Add the control to the TableCell
                    tc.Controls.Add(a);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }
        }

问题是当前月份即5月份未显示31天。无法思考如何解决此问题

enter image description here

1 个答案:

答案 0 :(得分:3)

你的问题是:

if (startOfCalender1 == DaysinLastMonth + 1)

在此示例中,DaysinLastMonth ==四月份的天数== 30

因此,上面的if语句转换为:

if (startOfCalender1 == 31)
{
       startOfCalender1 = 1;
}

您希望在从4月30日过渡到5月1日时使用该逻辑。

从5月30日过渡到5月31日时,您不想使用此逻辑。