带Check的当月日期
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Web.UI.HtmlControls; 8 9 namespace CheckDate_Simple 10 { 11 public partial class WebForm1 : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 InitControl(); 16 } 17 18 private void InitControl() 19 { 20 DateTime dt = Convert.ToDateTime(DateTime.Now.Year+"-" + DateTime.Now.Month+"-01"); 21 int monthDays = DateTime.DaysInMonth(dt.Year, dt.Month); 22 HtmlTable tb = new HtmlTable(); 23 tb.ID = "tbWeekDate"; 24 tb.CellPadding = 5; 25 tb.CellSpacing = 0; 26 InitTableHeader(tb);//新增列标题 27 int iCell = 0; 28 bool isNewRow = true; 29 HtmlTableCell tc = null; 30 HtmlTableRow tr = null; 31 CheckBox cb = null; 32 Label lbl = null; 33 for (int i = 0; i < monthDays; i++) 34 { 35 if (isNewRow) 36 { 37 tr = new HtmlTableRow(); 38 tb.Rows.Add(tr); 39 iCell = 0; 40 } 41 if (i == 0) 42 { 43 for (int j = 0; j < Convert.ToInt32(dt.DayOfWeek); j++) 44 { 45 tc = new HtmlTableCell(); 46 tc.InnerText = " "; 47 tr.Cells.Add(tc); 48 iCell++; 49 } 50 tc = new HtmlTableCell(); 51 cb = new CheckBox(); 52 lbl = new Label(); 53 lbl.Text = dt.ToString("yyyy-MM-dd"); 54 lbl.ID = "lbl_" + i; 55 56 cb.ID = "cb_" + i; 57 //cb.Attributes.Add("onclick", "checkDateChoose(this)"); 58 cb.Checked = true; 59 tc.Controls.Add(cb); 60 tc.Controls.Add(lbl); 61 tr.Cells.Add(tc); 62 iCell++; 63 } 64 else if (i == monthDays - 1)//最后一行 65 { 66 tc = new HtmlTableCell(); 67 cb = new CheckBox(); 68 lbl = new Label(); 69 lbl.Text = dt.AddDays(i).ToString("yyyy-MM-dd"); 70 lbl.ID = "lbl_" + i; 71 cb.ID = "cb_" + i; 72 //cb.Attributes.Add("onclick", "checkDateChoose(this)"); 73 cb.Checked = true; 74 tc.Controls.Add(cb); 75 tc.Controls.Add(lbl); 76 tr.Cells.Add(tc); 77 iCell++; 78 for (int j = 0; j < 6 - Convert.ToInt32(dt.AddDays(i).DayOfWeek); j++) 79 { 80 tc = new HtmlTableCell(); 81 tc.InnerText = " "; 82 tr.Cells.Add(tc); 83 iCell++; 84 } 85 } 86 else 87 { 88 tc = new HtmlTableCell(); 89 cb = new CheckBox(); 90 lbl = new Label(); 91 lbl.Text = dt.AddDays(i).ToString("yyyy-MM-dd"); 92 lbl.ID = "lbl_" + i; 93 cb.ID = "cb_" + i; 94 //cb.Attributes.Add("onclick", "checkDateChoose(this)"); 95 cb.Checked = true; 96 tc.Controls.Add(cb); 97 tc.Controls.Add(lbl); 98 tr.Cells.Add(tc); 99 iCell++; 100 } 101 isNewRow = iCell == 7; 102 } 103 this.div.Controls.Add(tb); 104 } 105 106 private void InitTableHeader(HtmlTable tb) 107 { 108 string[] strWeeks = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 109 HtmlTableRow th = new HtmlTableRow(); 110 for (int i = 0; i < strWeeks.Count(); i++) 111 { 112 th.BgColor = "#fff0f0"; 113 HtmlTableCell tc1 = new HtmlTableCell(); 114 CheckBox cb1 = new CheckBox(); 115 cb1.ID = "cball_" + i; 116 Label lbl1 = new Label(); 117 lbl1.Text = strWeeks[i].ToString(); 118 tc1.Controls.Add(cb1); 119 tc1.Controls.Add(lbl1); 120 th.Cells.Add(tc1); 121 } 122 tb.Rows.Add(th); 123 } 124 125 } 126 }
因业务需要,带Check功能的日历代码。