asp.net 写的日历程序,用在日常安排和任务管理, 代码里面包含一个比较经典的日历生成算法,希望大家能看懂。...


asp.net c# 写的日历程序,用在日常安排和任务管理
Default.aspx
  1 ExpandedBlockStart.gif ContractedBlock.gif <% @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  %>
  2
  3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  4 < html  xmlns ="http://www.w3.org/1999/xhtml" >
  5 < head  runat ="server" >
  6      < title ></ title >
  7 ExpandedBlockStart.gifContractedBlock.gif     < style >
  8        body
  9ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 10            font-size: 12px;
 11            cursor: default;
 12        }

 13        .trWeek
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 15        }

 16        .txtYear
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 18            font-size: 12px;
 19            background-color: Transparent;
 20            height: 20px;
 21            color: White;
 22            vertical-align: bottom;
 23            width: 34px;
 24            border: 1px solid gray;
 25            cursor: hand;
 26        }

 27        .txtMonth
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 29            font-size: 12px;
 30            background-color: Transparent;
 31            height: 20px;
 32            color: White;
 33            vertical-align: bottom;
 34            width: 20px;
 35            border: 1px solid gray;
 36            cursor: hand;
 37        }

 38        .tableCalendar
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 40            font-size: 12px;
 41            border: 1px solid black;
 42        }

 43        td
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 45        }

 46        .tdDay
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 48            background-color: #d0d0d0;
 49            border: 1px solid gray;
 50            text-align: center;
 51            height: 24px;
 52            cursor: hand;
 53            text-decoration: underline;
 54        }

 55        .tdCurrentDay
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 57            background-color: Gold;
 58            border: 1px solid red;
 59            text-align: center;
 60            cursor: hand;
 61            text-decoration: underline;
 62        }

 63        .tdNowDay
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 65            background-color: Yellow;
 66            border: 1px solid red;
 67            text-align: center;
 68            cursor: hand;
 69            text-decoration: underline;
 70        }

 71        .tdNotCurrentMonthDay
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 73            background-color: #f0f0f0;
 74            text-align: center;
 75            height: 24px;
 76            cursor: hand;
 77            text-decoration: underline;
 78        }

 79        .tdCurrentDate
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 81            background-color: #00125e;
 82            color: white;
 83            text-align: center;
 84            background-image: url(images/tdCurrentDate.png?<%=DateTime.Now.ToString("yyyyMMdd")%>);
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            height: 27px; /**//* text-decoration:underline;*/
 86        }

 87        .tdCurrentDateArrowBack
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 89            background-color: #00125e;
 90            color: white;
 91            text-align: center;
 92            background-image: url(images/tdCurrentDate.png?<%=DateTime.Now.ToString("yyyyMMdd")%>);
 93            cursor: hand;
 94            text-decoration: underline;
 95        }

 96        .tdCurrentDateArrowForward
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
 98            background-color: #00125e;
 99            color: white;
100            text-align: center;
101            background-image: url(images/tdCurrentDate.png?<%=DateTime.Now.ToString("yyyyMMdd")%>);
102            cursor: hand;
103            text-decoration: underline;
104        }

105        .tdWeekTitle
106ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
107            background-color: gray;
108            color: white;
109            text-align: center;
110        }

111        .divSelectMonth
112ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
113            display: none;
114            width: 64px;
115        }

116        .divSelectYear
117ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
118            display: none;
119            width: 50px;
120        }

121        .txtNow
122ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
123            cursor: hand;
124            border: 1px solid gray;
125            background-color: Transparent;
126            height: 20px;
127            width: 28px;
128            color: White;
129        }

130        .txtOneWord
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
132            cursor: default;
133            border: 0px;
134            background-color: Transparent;
135            height: 20px;
136            width: 14px;
137            color: White;
138        }

139        .tdSelectDate
140ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
141            cursor: hand;
142            background-color: White;
143            border: 1px solid gray;
144            text-align: center;
145        }

146        .tdSelectDateHover
147ExpandedSubBlockStart.gifContractedSubBlock.gif        {}{
148            cursor: hand;
149            background-color: #d0d0d0;
150            border: 1px solid gray;
151            text-align: center;
152        }

153    
</ style >
154
155 ExpandedBlockStart.gifContractedBlock.gif     < script  language ="javascript"  type ="text/javascript" >
156
157ExpandedSubBlockStart.gifContractedSubBlock.gif        function changeDate(oTDDate) {
158            var hidMethod = $("hidMethod");
159            var hidCurrentSelectedDate = $("hidCurrentSelectedDate");
160            hidMethod.value = "ChangeDate";
161            hidCurrentSelectedDate.value = oTDDate.date;
162            form1.submit();
163        }

164ExpandedSubBlockStart.gifContractedSubBlock.gif        function changeMonth(monthValue) {
165            var divSelectMonth = $("divSelectMonth");
166ExpandedSubBlockStart.gifContractedSubBlock.gif            if (divSelectMonth) { divSelectMonth.style.display = 'none'; }
167            var txtYear = $('txtYear');
168            var selectedDate = new Object()
169            selectedDate.date = txtYear.value + '-' + (monthValue.length < 2 ? "0" + monthValue : monthValue) + '-01';
170            changeDate(selectedDate);
171        }

172ExpandedSubBlockStart.gifContractedSubBlock.gif        function changeYear(yearValue) {
173            var divSelectYear = $("divSelectYear");
174ExpandedSubBlockStart.gifContractedSubBlock.gif            if (divSelectYear) { divSelectYear.style.display = 'none'; }
175            var txtMonth = $('txtMonth');
176            var selectedDate = new Object()
177            selectedDate.date = yearValue + '-' + txtMonth.value + '-01';
178            changeDate(selectedDate);
179        }

180ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectNowYear() {
181            var hidNowYear = $("hidNowYear");
182ExpandedSubBlockStart.gifContractedSubBlock.gif            if (hidNowYear) {
183                var txtMonth = $("txtMonth");
184                var selectedDate = new Object()
185                selectedDate.date = hidNowYear.value + '-' + txtMonth.value + '-01';
186                changeDate(selectedDate);
187            }

188        }

189ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectNow() {
190            var hidNowDate = $("hidNowDate");
191            var selectedDate = new Object()
192            selectedDate.date = hidNowDate.value;
193            changeDate(selectedDate);
194        }

195ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectNowMonth() {
196            var hidNowMonth = $("hidNowMonth");
197ExpandedSubBlockStart.gifContractedSubBlock.gif            if (hidNowMonth) {
198                var txtYear = $("txtYear");
199                var selectedDate = new Object()
200                selectedDate.date = txtYear.value + '-' + hidNowMonth.value + '-01';
201                changeDate(selectedDate);
202            }

203        }

204
205ExpandedSubBlockStart.gifContractedSubBlock.gif        function hideDivSelectMonth() {
206            var divSelectMonth = $("divSelectMonth");
207            divSelectMonth.style.display = 'none';
208        }

209ExpandedSubBlockStart.gifContractedSubBlock.gif        function hideDivSelectYear() {
210            var divSelectYear = $("divSelectYear");
211            divSelectYear.style.display = 'none';
212
213        }

214ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectYear(oTxt) {
215            hideDivSelectMonth();
216            var divSelectYear = $("divSelectYear");
217            var txtYear = $("txtYear");
218            var beginYear = parseInt(txtYear.value) - 3;
219            var endYear = parseInt(txtYear.value) + 3;
220ExpandedSubBlockStart.gifContractedSubBlock.gif            if (divSelectYear) {
221                var selectYearHtml = "<table style='font-size:12px;filter:alpha(Opacity=70)'><tr><td style='background-Color:#000000;height:2px;'></td></tr>"
222ExpandedSubBlockStart.gifContractedSubBlock.gif                for (var i = beginYear; i < endYear; i++{
223ExpandedSubBlockStart.gifContractedSubBlock.gif                    if (txtYear.value != i) {
224                        selectYearHtml += "<tr><td οnclick='changeYear(this.yearvalue)' yearvalue='" + i + "' class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"'>" + i + "年</td></tr>"
225                    }

226ExpandedSubBlockStart.gifContractedSubBlock.gif                    else {
227                        selectYearHtml += "<tr><td οnclick='changeYear(this.yearvalue)' yearvalue='" + i + "'  class='tdSelectDateHover' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDateHover\"'>" + i + "年</td></tr>"
228                    }

229                }

230                selectYearHtml += "<tr><td οnclick='selectNowYear();'  class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"'>今 年</td></tr>";
231                selectYearHtml += "<tr><td οnclick='hideDivSelectYear();'  class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"' title='关闭选择菜单'>关 闭</td></tr>";
232                selectYearHtml += "<tr><td style='background-Color:#000000;height:2px;'></td></tr>";
233                selectYearHtml += "</table>";
234                divSelectYear.innerHTML = selectYearHtml;
235                divSelectYear.style.border = "1px solid gray";
236                divSelectYear.style.backgroundColor = "WhiteSmoke";
237                divSelectYear.style.position = "absolute";
238                divSelectYear.style.zIndex = "1000";
239                divSelectYear.style.display = "block";
240
241            }

242        }

243ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectMonth(oTxt) {
244            hideDivSelectYear();
245            var divSelectMonth = $("divSelectMonth");
246            var txtMonth = $("txtMonth");
247ExpandedSubBlockStart.gifContractedSubBlock.gif            if (divSelectMonth) {
248                var selectMonthHtml = "<table style='font-size:12px;filter:alpha(Opacity=70)'><tr><td style='background-Color:#000000;height:2px;' colspan='2'></td></tr>"
249ExpandedSubBlockStart.gifContractedSubBlock.gif                for (var i = 1; i < 13; i++{
250                    var si = (i.toString().length > 1 ? i : "0" + i);
251ExpandedSubBlockStart.gifContractedSubBlock.gif                    if ((i + 1% 2 == 0{ selectMonthHtml += "<tr>"; }
252ExpandedSubBlockStart.gifContractedSubBlock.gif                    if (txtMonth.value != si) {
253                        selectMonthHtml += "<td οnclick='changeMonth(this.monthvalue)' monthvalue='" + si + "' class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"'>" + si + "月</td>"
254                    }

255ExpandedSubBlockStart.gifContractedSubBlock.gif                    else {
256                        selectMonthHtml += "<td οnclick='changeMonth(this.monthvalue)' monthvalue='" + si + "' class='tdSelectDateHover' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDateHover\"'>" + si + "月</td>"
257                    }

258ExpandedSubBlockStart.gifContractedSubBlock.gif                    if ((i) % 2 == 0{ selectMonthHtml += "</tr>"; }
259                }

260                selectMonthHtml += "<tr><td colspan='2' οnclick='selectNowMonth()' class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"'>本  月</td></tr>";
261                selectMonthHtml += "<tr><td colspan='2' οnclick='hideDivSelectMonth();' class='tdSelectDate' οnmοuseοver='this.className=\"tdSelectDateHover\"' οnmοuseοut='this.className=\"tdSelectDate\"' title='关闭选择菜单'>关  闭</td></tr>";
262                selectMonthHtml += "<tr><td colspan='2'  style='background-Color:#000000;height:2px;'></td></tr>";
263                selectMonthHtml += "</table>";
264
265                divSelectMonth.innerHTML = selectMonthHtml;
266                divSelectMonth.style.border = "1px solid gray";
267                divSelectMonth.style.backgroundColor = "WhiteSmoke";
268                divSelectMonth.style.position = "absolute";
269                divSelectMonth.style.zIndex = "1000";
270                divSelectMonth.style.display = "block";
271
272            }

273        }

274ExpandedSubBlockStart.gifContractedSubBlock.gif        function selectDate(oTDDate) {
275            //alert(oTDDate.date);
276        }

277ExpandedSubBlockStart.gifContractedSubBlock.gif        function $(elementID) {
278            return document.getElementById(elementID);
279        }

280    
</ script >
281
282 </ head >
283 < body  onselectstart ="return false;" >
284      < form  id ="form1"  runat ="server" >
285      < input  type ="hidden"  name ="hidMethod"  id ="hidMethod"  runat ="server"   />
286      < input  type ="hidden"  name ="hidCurrentSelectedDate"  id ="hidCurrentSelectedDate"  runat ="server"   />
287      < input  type ="hidden"  name ="hidNowYear"  id ="hidNowYear"  runat ="server"   />
288      < input  type ="hidden"  name ="hidNowMonth"  id ="hidNowMonth"  runat ="server"   />
289      < input  type ="hidden"  name ="hidNowDate"  id ="hidNowDate"  runat ="server"   />
290      < div  id ="divCalendar"  runat ="server"  enableviewstate ="false" ></ div >
291      </ form >
292 </ body >
293 </ html >
294

后台代码
Default.aspx.cs
 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Linq;
 4 using  System.Web;
 5 using  System.Web.UI;
 6 using  System.Web.UI.WebControls;
 7
 8 public   partial   class  _Default : System.Web.UI.Page
 9 ExpandedBlockStart.gifContractedBlock.gif {
10    protected void Page_Load(object sender, EventArgs e)
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        hidNowDate.Value = System.DateTime.Now.ToString("yyyy-MM-dd");
13        hidNowMonth.Value = DateTime.Now.ToString("MM");
14        hidNowYear.Value = DateTime.Now.ToString("yyyy");
15        if (hidMethod.Value.ToLower() == "changedate")
16ExpandedSubBlockStart.gifContractedSubBlock.gif        {
17            ChangeDate(Convert.ToDateTime(hidCurrentSelectedDate.Value));
18        }

19        else
20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
21            ChangeDate(DateTime.Now);
22        }

23    }

24    private void ChangeDate(DateTime CurrentDateTime)
25ExpandedSubBlockStart.gifContractedSubBlock.gif    {
26        int UnitCount = 42//42个单元格
27
28        DateTime CurrentMonthFirstDay = Convert.ToDateTime(CurrentDateTime.ToString("yyyy-MM-01"));
29
30        DateTime CurrentMonthLastDay = CurrentMonthFirstDay.AddMonths(1).AddDays(-1);
31
32        int WeekDay = Convert.ToInt32(CurrentMonthFirstDay.DayOfWeek); //取得当前要显示的月份的1号是在星期几 
33        int CurrentMonthDaysCount = ((TimeSpan)(CurrentMonthLastDay - CurrentMonthFirstDay.AddDays(-1))).Days; //取得当前要显示的月份一共有多少天
34        int SubUnitCount = UnitCount - CurrentMonthDaysCount - WeekDay;
35        DateTime BeginDateTime = CurrentMonthFirstDay.AddDays((WeekDay) * -1);
36        DateTime EndDateTime = CurrentMonthLastDay.AddDays(SubUnitCount);
37
38        Response.Write(string.Format("该页开始日期是:{0} 结束日期是:{1}<br>", BeginDateTime.ToString("yyyy-MM-dd"), EndDateTime.ToString("yyyy-MM-dd")));
39        Response.Write(CurrentDateTime.ToString("yyyy-MM-dd"+ "<br>");
40        Response.Write("取得当前要显示的月份的1号是在星期几: " + (DayOfWeek)WeekDay + "<br>");
41        Response.Write("取得当前要显示的月份一共有多少天: " + CurrentMonthDaysCount + "<br>");
42
43        string CalendarHtml = string.Format("<table class='tableCalendar'>\r\n" +
44    "<tr>" +
45    "<td class='tdCurrentDateArrowBack' title='上月'  οnclick='changeDate(this)' date='" + CurrentDateTime.AddMonths(-1).ToString("yyyy-MM-01"+ "'><img src='Images/tdCurrentDateArrowBack.png?" + DateTime.Now.ToString("yyyyMMdd"+ "' /></td>" +
46    "<td id='tdCurrentDate' colspan='5' class='tdCurrentDate' date='" + DateTime.Now.ToString("yyyy-MM-dd"+ "'><div class='divSelectYear' id='divSelectYear' title='选择年份'></div><input type='text' title='选择年份'  maxlength='4' value='{0}' class='txtYear' id='txtYear' name='txtYear' readonly='yes' οnclick='selectYear(this)' /><input type='text' class='txtOneWord' value='年' readonly='yes' /><div title='选择月份' class='divSelectMonth' id='divSelectMonth'></div><input title='选择月份'  type='text' maxlength='2' value='{1}' class='txtMonth' id='txtMonth' name='txtMonth' readonly='yes' οnclick='selectMonth(this)' /><input type='text' class='txtOneWord' value='月' readonly='yes' /><input type='text' οnclick='selectNow()'  class='txtNow' id='txtNow' name='txtNow' value='今日' title='选择今日' readonly='yes'  /></td>" +
47    "<td class='tdCurrentDateArrowForward' title='下月'><img src='Images/tdCurrentDateArrowForward.png?" + DateTime.Now.ToString("yyyyMMdd"+ "'  οnclick='changeDate(this)' date='" + CurrentDateTime.AddMonths(1).ToString("yyyy-MM-01"+ "' /></td></tr>\r\n",
48    CurrentDateTime.ToString("yyyy"), CurrentDateTime.ToString("MM"));
49        CalendarHtml += "<tr><td class='tdWeekTitle'>周日</td><td class='tdWeekTitle'>周一</td><td class='tdWeekTitle'>周二</td><td class='tdWeekTitle'>周三</td><td class='tdWeekTitle'>周四</td><td class='tdWeekTitle'>周五</td><td class='tdWeekTitle'>周六</td></tr>\r\n";
50        for (int i = 0; i < UnitCount; i++)
51ExpandedSubBlockStart.gifContractedSubBlock.gif        {
52            DateTime DrawCurrentDate = BeginDateTime.AddDays(i);
53            string ShowDay = DrawCurrentDate.ToString("dd");
54            string DrawCurrentDateString = DrawCurrentDate.ToString("yyyy-MM-dd");
55ExpandedSubBlockStart.gifContractedSubBlock.gif            if ((i) % 7 == 0{ CalendarHtml += "<tr>"; }
56            if (DrawCurrentDate.ToString("MM").Equals(CurrentDateTime.ToString("MM")))
57ExpandedSubBlockStart.gifContractedSubBlock.gif            {
58                if (DrawCurrentDateString.Equals(CurrentDateTime.ToString("yyyy-MM-dd")))
59ExpandedSubBlockStart.gifContractedSubBlock.gif                {
60                    CalendarHtml += "<td class='tdCurrentDay' date='" + DrawCurrentDateString + "'>" + ShowDay + "</td>";
61                }

62                else if (DrawCurrentDateString.Equals(DateTime.Now.ToString("yyyy-MM-dd")))
63ExpandedSubBlockStart.gifContractedSubBlock.gif                {
64                    CalendarHtml += "<td class='tdNowDay' date='" + DrawCurrentDateString + "'  οnclick='changeDate(this)'>" + ShowDay + "</td>";
65                }

66                else
67ExpandedSubBlockStart.gifContractedSubBlock.gif                {
68                    CalendarHtml += "<td class='tdDay' date='" + DrawCurrentDateString + "'  οnclick='changeDate(this)'>" + ShowDay + "</td>";
69                }

70            }

71            else
72ExpandedSubBlockStart.gifContractedSubBlock.gif            {
73                CalendarHtml += "<td class='tdNotCurrentMonthDay'  οnclick='changeDate(this)' date='" + DrawCurrentDateString + "'>" + DrawCurrentDate.ToString("dd"+ "</td>";
74            }

75ExpandedSubBlockStart.gifContractedSubBlock.gif            if ((i + 1% 7 == 0{ CalendarHtml += "</tr>"; }
76
77        }

78        CalendarHtml += "</table>";
79        
80        divCalendar.InnerHtml = CalendarHtml;
81    }

82}

转载于:https://www.cnblogs.com/karon/archive/2009/09/15/1566756.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值