1.关于万年历
这是一个非常简陋的万年历,C#的DateTime类提供了DayOfWeek属性,星期计算变得非常简单
2.ASP代码部分
BODY标签内代码
3.C#代码部分
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page{ ////// 提交 /// /// /// protected void btnSubmit_Click(object sender, EventArgs e) { int iYearPart1 = int.Parse(Century.Value); int iYearPart2 = int.Parse(Decade.Value); int iYearPart3 = int.Parse(Year.Value); int iYear = iYearPart1 + iYearPart2 + iYearPart3; int iMonth = int.Parse(Month.Value); //类似MessageBox的效果 //注意:1.不要使用 ButtonXXX.Attributes.Add("onclick","XXX") 重复添加按钮事件 // 2.Page.RegisterStartupScript被标注为:“已过时” ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("", iYear.ToString(), iMonth.ToString())); // string[][] calendar = new string[][] { new string[7] { "-", "-", "-", "-", "-", "-", "-" }, new string[7] { "-", "-", "-", "-", "-", "-", "-" }, new string[7] { "-", "-", "-", "-", "-", "-", "-" }, new string[7] { "-", "-", "-", "-", "-", "-", "-" }, new string[7] { "-", "-", "-", "-", "-", "-", "-" }, new string[7] { "-", "-", "-", "-", "-", "-", "-" } }; DateTime dtFirst = new DateTime(iYear, iMonth, 1); int x = 0, y = 0; string s = dtFirst.DayOfWeek.ToString(); switch (dtFirst.DayOfWeek.ToString()) { case "Sunday": y = 0; break; case "Monday": y = 1; break; case "Tuesday": y = 2; break; case "Wednesday": y = 3; break; case "Thursday": y = 4; break; case "Friday": y = 5; break; case "Saturday": y = 6; break; } //计算指定月份的日期 DateTime dt = dtFirst; while(true) { calendar[x][y] = dt.Day.ToString(); dt = dt.AddDays(1); if (dt.Month == iMonth) { if (++y > 6) { x++; y = 0; } } else { break; } } //HtmlTableRow 和 HtmlTableCell 需要 System.Web.UI.HtmlControls //上部:标题 HtmlTableRow r1 = new HtmlTableRow(); HtmlTableCell c1 = new HtmlTableCell(); c1.Controls.Add(new LiteralControl("万年历")); c1.ColSpan = 7; //合并单元格,colspan属性可以实现单元格跨越多列 c1.Align = "Center"; //单元格内文字居中 r1.Cells.Add(c1); tbCalendar.Rows.Add(r1); //中部:日历表 int row = 0; int numrows = x + 1; //日历最多有六行 int numcells = 7; //每周有七天 for (int i = 0; i < numrows; i++) { HtmlTableRow r = new HtmlTableRow(); r.Height = "70"; row++; for (int j = 0; j < numcells; j++) { HtmlTableCell c = new HtmlTableCell(); //创建单元格对象 c.Controls.Add(new LiteralControl( calendar[i][j] != "-" ? calendar[i][j] : "")); if (calendar[i][j] == "-") { c.BgColor = "Gainsboro"; } else { c.BgColor = "LawnGreen"; } c.Align = "Center"; r.Cells.Add(c); //添加该单元格对象 } tbCalendar.Rows.Add(r);//添加行对 } //底部:其他信息 HtmlTableRow r2 = new HtmlTableRow(); HtmlTableCell c2 = new HtmlTableCell(); c2.Controls.Add(new LiteralControl("生成于:" + DateTime.Now.ToString())); c2.ColSpan = 7; //合并单元格,colspan属性可以实现单元格跨越多列 c2.Align = "Center"; r2.Cells.Add(c2); tbCalendar.Rows.Add(r2); }}
END