asp.net c# 写的日历程序,用在日常安排和任务管理
Default.aspx
  1<%@ 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    <style>
  8        body
  9        {
 10            font-size: 12px;
 11            cursor: default;
 12        }

 13        .trWeek
 14        {
 15        }

 16        .txtYear
 17        {
 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
 28        {
 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
 39        {
 40            font-size: 12px;
 41            border: 1px solid black;
 42        }

 43        td
 44        {
 45        }

 46        .tdDay
 47        {
 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
 56        {
 57            background-color: Gold;
 58            border: 1px solid red;
 59            text-align: center;
 60            cursor: hand;
 61            text-decoration: underline;
 62        }

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

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

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

 87        .tdCurrentDateArrowBack
 88        {
 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
 97        {
 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
106        {
107            background-color: gray;
108            color: white;
109            text-align: center;
110        }

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

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

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

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

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

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

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

164        function changeMonth(monthValue) {
165            var divSelectMonth = $("divSelectMonth");
166            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        }

172        function changeYear(yearValue) {
173            var divSelectYear = $("divSelectYear");
174            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        }

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

188        }

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

195        function selectNowMonth() {
196            var hidNowMonth = $("hidNowMonth");
197            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
205        function hideDivSelectMonth() {
206            var divSelectMonth = $("divSelectMonth");
207            divSelectMonth.style.display = 'none';
208        }

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

214        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;
220            if (divSelectYear) {
221                var selectYearHtml = "<table style='font-size:12px;filter:alpha(Opacity=70)'><tr><td style='background-Color:#000000;height:2px;'></td></tr>"
222                for (var i = beginYear; i < endYear; i++{
223                    if (txtYear.value != i) {
224                        selectYearHtml += "<tr><td onclick='changeYear(this.yearvalue)' yearvalue='" + i + "' class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDate\"'>" + i + "年</td></tr>"
225                    }

226                    else {
227                        selectYearHtml += "<tr><td onclick='changeYear(this.yearvalue)' yearvalue='" + i + "'  class='tdSelectDateHover' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDateHover\"'>" + i + "年</td></tr>"
228                    }

229                }

230                selectYearHtml += "<tr><td onclick='selectNowYear();'  class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDate\"'>今 年</td></tr>";
231                selectYearHtml += "<tr><td onclick='hideDivSelectYear();'  class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='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        }

243        function selectMonth(oTxt) {
244            hideDivSelectYear();
245            var divSelectMonth = $("divSelectMonth");
246            var txtMonth = $("txtMonth");
247            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>"
249                for (var i = 1; i < 13; i++{
250                    var si = (i.toString().length > 1 ? i : "0" + i);
251                    if ((i + 1% 2 == 0{ selectMonthHtml += "<tr>"; }
252                    if (txtMonth.value != si) {
253                        selectMonthHtml += "<td onclick='changeMonth(this.monthvalue)' monthvalue='" + si + "' class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDate\"'>" + si + "月</td>"
254                    }

255                    else {
256                        selectMonthHtml += "<td onclick='changeMonth(this.monthvalue)' monthvalue='" + si + "' class='tdSelectDateHover' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDateHover\"'>" + si + "月</td>"
257                    }

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

260                selectMonthHtml += "<tr><td colspan='2' onclick='selectNowMonth()' class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='this.className=\"tdSelectDate\"'>本  月</td></tr>";
261                selectMonthHtml += "<tr><td colspan='2' onclick='hideDivSelectMonth();' class='tdSelectDate' onmouseover='this.className=\"tdSelectDateHover\"' onmouseout='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        }

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

277        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
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Web;
 5using System.Web.UI;
 6using System.Web.UI.WebControls;
 7
 8public partial class _Default : System.Web.UI.Page
 9{
10    protected void Page_Load(object sender, EventArgs e)
11    {
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")
16        {
17            ChangeDate(Convert.ToDateTime(hidCurrentSelectedDate.Value));
18        }

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

23    }

24    private void ChangeDate(DateTime CurrentDateTime)
25    {
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='上月'  onclick='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' onclick='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' onclick='selectMonth(this)' /><input type='text' class='txtOneWord' value='月' readonly='yes' /><input type='text' onclick='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"+ "'  onclick='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++)
51        {
52            DateTime DrawCurrentDate = BeginDateTime.AddDays(i);
53            string ShowDay = DrawCurrentDate.ToString("dd");
54            string DrawCurrentDateString = DrawCurrentDate.ToString("yyyy-MM-dd");
55            if ((i) % 7 == 0{ CalendarHtml += "<tr>"; }
56            if (DrawCurrentDate.ToString("MM").Equals(CurrentDateTime.ToString("MM")))
57            {
58                if (DrawCurrentDateString.Equals(CurrentDateTime.ToString("yyyy-MM-dd")))
59                {
60                    CalendarHtml += "<td class='tdCurrentDay' date='" + DrawCurrentDateString + "'>" + ShowDay + "</td>";
61                }

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

66                else
67                {
68                    CalendarHtml += "<td class='tdDay' date='" + DrawCurrentDateString + "'  onclick='changeDate(this)'>" + ShowDay + "</td>";
69                }

70            }

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

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

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

82}
posted on 2009-09-15 09:47  凯龙  阅读(1130)  评论(0编辑  收藏  举报