带CheckBox的net版日历生成,欢迎高手拍砖
最近在做酒店管理方面的系统,其中用到了满房设置,即设置满房日期,需要checkbox勾选,日期跨度是3个月。实现的效果图如下:
本来想改calendar.js 实现的,可本人js太烂,索性就用C#自己写一个了,这中间也参照了网上的一些资料。话不多说了,下面边看代码边分析。
第一步:根据月份判断各月的天数。
private int getDaynum(int month, int year)
{
int daynum = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daynum = 31;
break;
case 4:
case 6:
case 9:
case 11:
daynum = 30;
break;
case 2:
if (isDateTime(year.ToString() + "-" + "2-29"))//判断是否为日期类型,因为2月份有时是28天和29的情况,所以此处要判断下某年的2月的具体天数为多少。
{
daynum = 29;
}
else
daynum = 28;
break;
}
return daynum;
}
第二步:用到的几个函数:月初,当前月的上一个月,当前月的下一个月(这里要展示3个月)关键代码如下:

//月初
protected int getstartdate(DateTime dt)
{
int dtemp = Convert.ToInt32(dt.AddDays(1 - dt.Day).DayOfWeek) + 1;
return dtemp;
}
//当前月的上一个月
protected DateTime lastdate(DateTime dt)
{
return dt.AddMonths(-1);
}
//当前月的下一个月
protected DateTime nextdate(DateTime dt)
{
return dt.AddMonths(1);
}
第三步:前面工作基本做好,现在开始生成日历了。关键代码如下:

/// <summary>
/// 生成日历
/// </summary>
/// <param name="dd">当前日期</param>
/// <param name="daynum1">当前月的天数</param>
/// <param name="icurrent1">当前是几日</param>
/// <returns></returns>
protected string getCalendar(DateTime dd, int daynum1, int icurrent1)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table width='180' border='1' align='center' cellpadding='1' cellspacing='1' bordercolor='#CCCCCC'>");
sb.Append("<tr>");
sb.Append("<td align='center' colspan='7'><table border='0' cellpadding='0' cellspacing='0' width='100%'>");
sb.Append("<tr>");
sb.Append("<td height='22' align='right'><a href='?date=" + lastdate(dd).ToShortDateString() + "&hid=" + Request.QueryString["hid"].ToString() + "&rid=" + Request.QueryString["rid"].ToString() + "'>上月</a></td> ");
sb.Append("<td align='center'><font color='666666'><b>" + dd.Year + "年" + dd.Month + "月" + "</b></font></td> ");
sb.Append("<td><a href='?date=" + nextdate(dd).ToShortDateString() + "&hid=" + Request.QueryString["hid"].ToString() + "&rid=" + Request.QueryString["rid"].ToString() + "'>下月</a></td> ");
sb.Append("</tr></table></td></tr>");
sb.Append("<tr>");
sb.Append("<td width='25' height='22' align='center'><font color='d08c00'><b>日</b></font> </td>");
sb.Append("<td width='25' align='center'><b><font color='666666'>一</font></b> </td> ");
sb.Append("<td width='25' align='center'><b><font color='666666'>二</font></b> </td> ");
sb.Append("<td width='25' align='center'><b><font color='666666'>三</font></b> </td> ");
sb.Append("<td width='25' align='center'><b><font color='666666'>四</font></b> </td> ");
sb.Append("<td width='25' align='center'><b><font color='666666'>五</font></b> </td> ");
sb.Append("<td width='25' align='center'><b><font color='d08c00'>六</font></b> </td> ");
sb.Append("</tr>");
if (idow != 1)
{
sb.Append("<tr>");
for (iposition = 1; iposition < idow; iposition++)
{
sb.Append("<td> </td>");
}
}
icurrent1 = 1;
iposition = idow;
while (icurrent1 <= daynum1)
{
if (iposition == 1)
{
sb.Append("<tr>");
}
Ct_room roomy = (Ct_room)ViewState["room"];
string datey = dd.Year + "-" + dd.Month + "-" + icurrent1;
string ppdate = datey + ",";//匹配日期
if (roomy.R_fulldate .Contains(ppdate )==true)//假如当前时间在满房日期段内,checkbox选中。
{
sb.Append("<td height=18 align=center>" + icurrent1 + "<br><input type=checkbox name=mfdate checked='selected' value=" + datey + "></td>");
}
else
{
sb.Append("<td height=18 align=center>" + icurrent1 + "<br><input type=checkbox name=mfdate value=" + datey + "></td>");
}
if (iposition == 7)
{
sb.Append("</tr>");
iposition = 0;
}
icurrent1++;
iposition++;
}
if (iposition != 1)
{
while (iposition <= 7)
{
sb.Append("<td> </td>");
iposition++;
}
sb.Append("</tr>");
}
sb.Append("</table>");
return sb.ToString();
}
前台调用:直接调用函数 getCalendar即可。调用例子如下:
cal.InnerHtml = getCalendar(System.DateTime.Now.AddMonths(1), getDaynum(System.DateTime.Now.AddMonths (1).Month, System.DateTime.Now.AddMonths (1).Year),getstartdate(System.DateTime.Now.AddMonths(1)) );
显示效果为:
不足之处:如果能做成控件的就好了。另外,是不是还要弄个异步刷新啥的。欢迎高手给予指导,多拍拍砖。。。。
-->
标签:
net版日历
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端