首先是前台代码:

<style> .zhoumo { background-color: Red; color: White; } </style> <script language="javascript" type="text/javascript"> //第一个参数代表是几号 function changeValue(obj) { $.ajax({ type: "POST", url: "DoWith.ashx", data: "workMethod="+ $("#td"+obj).attr("class")+"&year="+$("#ddlYear").val()+"&month="+$("#ddlMonth").val()+"&day="+obj, success: function(msg){ $("#td"+obj).toggleClass("zhoumo"); }}); } </script>
<table width="98%" align="center" border="0" cellpadding="4" cellspacing="1" bgcolor="#CBD8AC" style="margin-bottom: 8px"> <tr bgcolor="#EEF4EA"> <td colspan="2" background="skin/p_w_picpaths/frame/wbg.gif" class='title'> <span>节假日设置</span> </td> </tr> <tr bgcolor="#FFFFFF"> <td width="75%" bgcolor="#FFFFFF"> &nbsp; <asp:DropDownList ID="ddlYear" runat="server"> </asp:DropDownList> &nbsp; <asp:DropDownList ID="ddlMonth" runat="server"> </asp:DropDownList> </td> <td width="25%" bgcolor="#FFFFFF" style="text-align: center;"> <asp:Button ID="btnOK" runat="server" Text="转到" OnClick="btnOK_Click" /> </td> </tr> </table> </form> <table align="center" bgcolor="#D1DDAA" border="0" cellpadding="2" cellspacing="1" style="margin-top: 8px; " width="98%"> <tr align="center" bgcolor="#E7E7E7" height="22"> <td width="15%"> 星期日 </td> <td width="14%"> 星期一 </td> <td width="14%"> 星期二 </td> <td width="14%"> 星期三 </td> <td width="14%"> 星期四 </td> <td width="14%"> 星期五 </td> <td width="15%"> 星期六 </td> </tr> <%= strTable%> </table>然后是后台代码:


public string strTable = string.Empty; private readonly Base.BLL.WorkDaySet bllWorkDaySet = new Base.BLL.WorkDaySet(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlYear.AppendDataBoundItems = true; ddlYear.Items.Clear(); for (int i = 2010; i < 2020; i++) { ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString())); } ddlYear.AppendDataBoundItems = true; ddlMonth.AppendDataBoundItems = true; ddlMonth.Items.Clear(); for (int j = 1; j < 13; j++) { ddlMonth.Items.Add(new ListItem(j.ToString(), j.ToString())); } ddlMonth.AppendDataBoundItems = true; ddlYear.SelectedValue = DateTime.Now.Year.ToString(); ddlMonth.SelectedValue = DateTime.Now.Month.ToString(); strTable = getStr(); } } public string getStr() { //第一步获取该月有多少天,以及最后一天是是星期几 DateTime dtMonthEnd = Convert.ToDateTime(ddlYear.SelectedValue + "-" + ddlMonth.SelectedValue + "-1 00:00:00").AddMonths(1).AddDays(-1); int sum = dtMonthEnd.Day; int weekOfdayEnd = (int)dtMonthEnd.DayOfWeek;//看最后一天是星期几,用数字0-6代替 //第二步拼接字符串 StringBuilder sbr = new StringBuilder(); //开始行 sbr.Append("<tr align=\"center\" bgcolor=\"#FFFFFF\" height=\"22\">"); //填充空白的地方 DateTime dtTd = Convert.ToDateTime(ddlYear.SelectedValue + "-" + ddlMonth.SelectedValue + "-1 00:00:00"); for (int h = 0; h < (int)dtTd.DayOfWeek; h++) { sbr.Append(" <td ></td>"); } DateTime dtI; for (int i = 1; i <= sum; i++)//i表示的是多少号 { dtI = Convert.ToDateTime(ddlYear.SelectedValue + "-" + ddlMonth.SelectedValue + "-" + i.ToString() + " 00:00:00"); sbr.Append("<td style=\"cursor:pointer;\" onmousemove=\"this.bgColor='#FCFDEE';\" onmouseout=\"this.bgColor='#FFFFFF';\" id=\"td" + i.ToString() + "\" "); Base.Model.WorkDaySet model = bllWorkDaySet.GetModel(dtI); if (model.DID != 0) { sbr.Append("class=\"zhoumo\" "); } sbr.Append(" + i.ToString() + ")\">" + i.ToString() + "</td>"); if ((int)dtI.DayOfWeek == 6) { if (weekOfdayEnd == 6 && i== sum) sbr.Append("</tr>"); else sbr.Append("</tr><tr align=\"center\" bgcolor=\"#FFFFFF\" height=\"22\" >"); } } for (int j = weekOfdayEnd; j < 6; j++) { sbr.Append(" <td ></td>"); } if (weekOfdayEnd != 6) sbr.Append("</tr>"); return sbr.ToString(); }