zoukankan      html  css  js  c++  java
  • 带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)) );

    显示效果为:

     

    不足之处:如果能做成控件的就好了。另外,是不是还要弄个异步刷新啥的。欢迎高手给予指导,多拍拍砖。。。。

                      -->

    作者:JaryLeely
    谁说30岁是个坎?扯淡……
  • 相关阅读:
    editplus设置自动换行方法 editplus自动换行设置步骤
    com.google.gson.stream.MalformedJsonException
    系统架构设计师 笔记1
    微信文档采用第三方方式打开选择qq
    手机不弹toast解决方法
    Excel 如何快速切换到最后一行
    vue系列4:引入插件
    vue系列3:引入css和js
    vue系列2:npm install 命令详解
    vue系列1:创建vue项目
  • 原文地址:https://www.cnblogs.com/Jaryleely/p/1654823.html
Copyright © 2011-2022 走看看