asp.net萬年曆,以及表格邊框設定

请叫我七哥發表於2024-04-24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace DTcms.Web.admin
{
    public partial class test : System.Web.UI.Page
    {
        StringBuilder Table = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            Literal1.Text = Calendar(DateTime.Now.Year, DateTime.Now.Month);
        }
        #region 月份與星期
        private string[] ds_monthnames = {
                                        "一月", "二月", "三月", "四月", "五月", "六月",
                                        "七月", "八月", "九月", "十月", "十一月", "十二月"
                                     };
        private string[] ds_daynames =   {
                                        "週日", "週一", "週二", "週三", "週四", "週五", "週六"
                                     };
        #endregion

        #region Table操作
        /// <summary>
        /// 返回Table頭
        /// </summary>
        /// <returns></returns>
        private string TableTop()
        {
            return "<table border='0' cellspacing='1' class='ds_box'>" +
                   "<tr class='ds_subhead'>";

        }
        /// <summary>
        /// 返回Table中
        /// </summary>
        /// <returns></returns>
        private string TableMiddle()
        {
            return "</tr><tr class='ds_cell'>";
        }
        /// <summary>
        /// 返回Table尾
        /// </summary>
        /// <returns></returns>
        private string TableEnd()
        {
            return "</tr></table>";
        }
        public string TableWeek(string t)
        {
            return "<td class='ds_subhead'><center>" + t + "</center></td>";
        }
        private string retTD()
        {
            return "<td class='ds_cell'>&nbsp;</td>";
        }
        #endregion

        #region 返回當月第一天的位置
        private int ReturnTableWeek(int y, int m)
        {
            int i = 7;
            i = Convert.ToInt32(Convert.ToDateTime(y.ToString() + "-" + m.ToString() + "-" + "1" + " 00:00:00").DayOfWeek);
            switch (i)
            {
                case 1:
                    return 2; break;//星期一返回為第二個位置
                case 2:
                    return 3; break;
                case 3:
                    return 4; break;
                case 4:
                    return 5; break;
                case 5:
                    return 6; break;
                case 6:
                    return 7; break;
                case 7:
                    return 1; break;
                default:
                    return 1;
            }

        }
        #endregion

        #region 主函式
        public string Calendar(int year, int month)
        {
            int ds_c_month = month;
            int ds_c_year = year;
            Table.Append(TableTop());
            for (int week = 0; week <= 6; week++)
            {
                Table.Append(TableWeek(ds_daynames[week]));
            }
            Table.Append(TableMiddle());
            int ti = ReturnTableWeek(ds_c_year, ds_c_month);//得到當月第一天
            for (int k = 1; k < ti; k++)
            {
                Table.Append(retTD());
            }
            int tab = 0;
            //ti+ds_draw_calendar(ds_c_year,ds_c_month)
            for (int i = ti; i < ti + ds_draw_calendar(ds_c_year, ds_c_month); i++)
            {
                Table.Append(ds_Content_calendar(ds_c_year, ds_c_month, i - ti + 1));
                if (i % 7 == 0)
                {
                    Table.Append(TableMiddle());
                }
                tab = i;
            }
            while (tab % 7 != 0)
            {
                tab += 1;
                Table.Append(retTD());
            }
            Table.Append(TableEnd());
            return Table.ToString();

        }
        #endregion

        #region 返回本月的天數
        /// <summary>
        /// 返回本月的天數
        /// </summary>
        /// <param name="m"></param>
        /// <param name="y"></param>
        private int ds_draw_calendar(int y, int m)
        {
            int days = 0;
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
            {
                days = 31;
                return days;
            }
            else
            {
                if (m == 4 || m == 6 || m == 9 || m == 11)
                {
                    days = 30;
                    return days;
                }
                else
                {
                    //四年一閏,百年不閏,四百年再閏
                    if (m == 2)
                    {
                        days = (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? 29 : 28;
                        return days;
                    }
                    else
                    {
                        return days;
                    }
                }
            }
        }
        #endregion
        #region 返回具體一天 資料庫操作,需要將資料庫的資料放入表格,可在此函式中更改
        /// <summary>
        /// 返回具體一天
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="days"></param>
        /// <returns></returns>
        public virtual string ds_Content_calendar(int year, int month, int days)
        {
            if (days == DateTime.Now.Day)
            {
                return "<td style='color:green;'>" + days.ToString() + "</td>";
            }
            else
            {
                return "<td>" + days.ToString() + "</td>";
            }
        }
        #endregion
    }
}
<style>
.ds_box{border-collapse:collapse;width:100%}
.ds_box tr td{border:1px solid  #ccc;text-align:center}

</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

表格邊框設定

.ds_box{border-collapse:collapse;}
.ds_box tr td{border:1px solid  #ccc;}

相關文章