博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET学习之万年历
阅读量:6302 次
发布时间:2019-06-22

本文共 5109 字,大约阅读时间需要 17 分钟。

hot3.png

1.关于万年历

这是一个非常简陋的万年历,C#的DateTime类提供了DayOfWeek属性,星期计算变得非常简单

212243_KoAS_1425762.png

2.ASP代码部分

BODY标签内代码

    

ASP学习:万年历

世纪:
年代:
年:
月:
 

3.C#代码部分

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page{    ///     /// 提交    ///     ///     ///     protected void btnSubmit_Click(object sender, EventArgs e)    {        int iYearPart1 = int.Parse(Century.Value);        int iYearPart2 = int.Parse(Decade.Value);        int iYearPart3 = int.Parse(Year.Value);        int iYear = iYearPart1 + iYearPart2 + iYearPart3;        int iMonth = int.Parse(Month.Value);        //类似MessageBox的效果        //注意:1.不要使用 ButtonXXX.Attributes.Add("onclick","XXX") 重复添加按钮事件        //      2.Page.RegisterStartupScript被标注为:“已过时”        ClientScript.RegisterStartupScript(this.GetType(), "",            string.Format("",            iYear.ToString(), iMonth.ToString()));        //        string[][] calendar = new string[][]         {             new string[7] { "-", "-", "-", "-", "-", "-", "-" },            new string[7] { "-", "-", "-", "-", "-", "-", "-" },            new string[7] { "-", "-", "-", "-", "-", "-", "-" },            new string[7] { "-", "-", "-", "-", "-", "-", "-" },            new string[7] { "-", "-", "-", "-", "-", "-", "-" },            new string[7] { "-", "-", "-", "-", "-", "-", "-" }        };        DateTime dtFirst = new DateTime(iYear, iMonth, 1);        int x = 0, y = 0;        string s = dtFirst.DayOfWeek.ToString();        switch (dtFirst.DayOfWeek.ToString())        {            case "Sunday": y = 0; break;            case "Monday": y = 1; break;            case "Tuesday": y = 2; break;            case "Wednesday": y = 3; break;            case "Thursday": y = 4; break;            case "Friday": y = 5; break;            case "Saturday": y = 6; break;        }        //计算指定月份的日期        DateTime dt = dtFirst;        while(true)        {            calendar[x][y] = dt.Day.ToString();            dt = dt.AddDays(1);            if (dt.Month == iMonth)            {                if (++y > 6)                {                    x++;                    y = 0;                }            }            else            {                break;            }        }        //HtmlTableRow 和 HtmlTableCell 需要 System.Web.UI.HtmlControls        //上部:标题        HtmlTableRow r1 = new HtmlTableRow();        HtmlTableCell c1 = new HtmlTableCell();        c1.Controls.Add(new LiteralControl("万年历"));        c1.ColSpan = 7; //合并单元格,colspan属性可以实现单元格跨越多列        c1.Align = "Center"; //单元格内文字居中        r1.Cells.Add(c1);        tbCalendar.Rows.Add(r1);        //中部:日历表        int row = 0;        int numrows = x + 1;  //日历最多有六行        int numcells = 7; //每周有七天        for (int i = 0; i < numrows; i++)        {            HtmlTableRow r = new HtmlTableRow();            r.Height = "70";            row++;            for (int j = 0; j < numcells; j++)            {                HtmlTableCell c = new HtmlTableCell(); //创建单元格对象                c.Controls.Add(new LiteralControl(                    calendar[i][j] != "-" ? calendar[i][j] : ""));                if (calendar[i][j] == "-")                {                    c.BgColor = "Gainsboro";                }                else                {                    c.BgColor = "LawnGreen";                }                c.Align = "Center";                r.Cells.Add(c); //添加该单元格对象            }            tbCalendar.Rows.Add(r);//添加行对        }        //底部:其他信息        HtmlTableRow r2 = new HtmlTableRow();        HtmlTableCell c2 = new HtmlTableCell();        c2.Controls.Add(new LiteralControl("生成于:" + DateTime.Now.ToString()));        c2.ColSpan = 7; //合并单元格,colspan属性可以实现单元格跨越多列        c2.Align = "Center";        r2.Cells.Add(c2);        tbCalendar.Rows.Add(r2);    }}

END

转载于:https://my.oschina.net/Tsybius2014/blog/261300

你可能感兴趣的文章
CodeForces 443B Kolya and Tandem Repeat
查看>>
关于TouchEvent里面的touches,targetTouches,changedTouches的解释
查看>>
UVa 10382 - Watering Grass
查看>>
负margin
查看>>
eclipse的scala环境搭建
查看>>
UVA465:Overflow
查看>>
HTML5-placeholder属性
查看>>
CocoaPods安装和使用教程
查看>>
MAC-前端环境
查看>>
第五周总结报告
查看>>
先行进位加法器
查看>>
九、获取验证码
查看>>
如何修改java web的项目名
查看>>
printf()与 scanf()
查看>>
POJ 1654 area 解题
查看>>
查找整数(蓝桥杯)
查看>>
vscode常用快捷键及常用设置
查看>>
pinpoint改造支持查询
查看>>
JMeter基础之--元件的作用域与执行顺序
查看>>
day22-Model数据库操作补充之QuerySet方法详细
查看>>