提供一个日期选择器 --引用了一个脚本,所以不是原创
版权还是原作者的......偶声明!
源代码如下.
using System;
using System.Text;
namespace SasbyaWebControl
{
/// <summary>
/// sDateTime is a ASP.Net web control base on F.R.Huang(meizz) [ http://www.meizz.com/ mail: meizz@hzcnc.com 2002-10-8 ] 's work.
/// Developer: Zhou ShiYin (SaSBYa@sohu.com)
/// Date:2004-10-1
///
/// sDataTime
/// How to use this control:
///1.drop this control on you web page
///2.use the method targetTextBox to register you textbox control to make it show the datetimepicker.
///
/// Remember:
/// You can drop only 1 this control in ONE web page and register many controls.
///
///
/// sDateTime是基于 F.R.Huang(meizz) [ http://www.meizz.com/ mail: meizz@hzcnc.com 2002-10-8 ]的一个日期选择器,做的一个基于ASP.Net的日期选择器控件。
/// 改编者: SaSBYa(sasbya那个邮件符号sohu.com)
/// 完成日期:2004-10-08
/// 版权属于原作者!
///
/// 使用说明:
/// 拖到ASP.Net 页面上
/// 使用targetTextBox注册对应的textBox控件(可以注册多个)
///
/// 存在的问题:
/// 使用此控件的页面不可以使用smartNavigator
/// </summary>
public class sDateTime:System.Web.UI.WebControls.WebControl
{
public sDateTime()
{
}
protected override void OnLoad(EventArgs e)
{
Page.RegisterClientScriptBlock(this.ClientID+"SaSBYaDateTimePicker",ConstructScripts());
}
public void targetTextBox(System.Web.UI.WebControls.TextBox target)
{
target.Attributes.Add("onfocus","setday("+target.ClientID+");");
}
private string ConstructScripts()
{
StringBuilder myString=new StringBuilder();
myString.Append("<script language=javascript>\r\n");
myString.Append("<!--\r\n");
myString.Append("document.writeln('<div id=meizzDateLayer style=\"position: absolute; 142; height: 166; z-index: 9998; display: none\">');\r\n");
myString.Append("document.writeln('<span id=tmpSelectYearLayer style=\"z-index: 9999;position: absolute;top: 2; left: 18;display: none\"></span>');\r\n");
myString.Append("document.writeln('<span id=tmpSelectMonthLayer style=\"z-index: 9999;position: absolute;top: 2; left: 75;display: none\"></span>');\r\n");
myString.Append("document.writeln('<table border=0 cellspacing=1 cellpadding=0 width=142 height=160 bgcolor=#808080 onselectstart=\"return false\">');\r\n");
myString.Append("document.writeln(' <tr><td width=142 height=23 bgcolor=#FFFFFF><table border=0 cellspacing=1 cellpadding=0 width=140 height=23>');\r\n");
myString.Append("document.writeln(' <tr align=center><td width=20 align=center bgcolor=#808080 style=\"font-size:12px;cursor: hand;color: #FFD700\" ');\r\n");
myString.Append("document.writeln(' onclick=\"meizzPrevM()\" title=\"向前翻 月\" Author=meizz><b Author=meizz><<</b>');\r\n");
myString.Append("document.writeln(' </td><td width=100 align=center style=\"font-size:12px;cursor:default\" Author=meizz>');\r\n");
myString.Append("document.writeln(' <span Author=meizz id=meizzYearHead onclick=\"tmpSelectYearInnerHTML(this.innerText)\"></span> 年 <span');\r\n");
myString.Append("document.writeln(' id=meizzMonthHead Author=meizz onclick=\"tmpSelectMonthInnerHTML(this.innerText)\"></span> 月</td>');\r\n");
myString.Append("document.writeln(' <td width=20 bgcolor=#808080 align=center style=\"font-size:12px;cursor: hand;color: #FFD700\" ');\r\n");
myString.Append("document.writeln(' onclick=\"meizzNextM()\" title=\"往后翻 月\" Author=meizz><b Author=meizz>>></b></td></tr>');\r\n");
myString.Append("document.writeln(' </table></td></tr>');\r\n");
myString.Append("document.writeln(' <tr><td width=142 height=18 bgcolor=#808080>');\r\n");
myString.Append("document.writeln('<table border=0 cellspacing=0 cellpadding=0 width=140 height=1 style=\"cursor:default\">');\r\n");
myString.Append("document.writeln('<tr align=center><td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>日</td>');\r\n");
myString.Append("document.writeln('<td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>一</td><td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>二</td>');\r\n");
myString.Append("document.writeln('<td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>三</td><td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>四</td>');\r\n");
myString.Append("document.writeln('<td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>五</td><td style=\"font-size:12px;color:#FFFFFF\" Author=meizz>六</td></tr>');\r\n");
myString.Append("document.writeln('</table></td></tr><!-- Author:F.R.Huang(meizz) http://www.meizz.com/ mail: meizz@hzcnc.com 2002-10-8 -->');\r\n");
myString.Append("document.writeln(' <tr><td width=142 height=120>');\r\n");
myString.Append("document.writeln(' <table border=0 cellspacing=1 cellpadding=0 width=140 height=120 bgcolor=#FFFFFF>');\r\n");
myString.Append("var n=0; for (j=0;j<5;j++){ document.writeln (' <tr align=center>'); for (i=0;i<7;i++){\r\n");
myString.Append("document.writeln('<td width=20 height=20 id=meizzDay'+n+' style=\"font-size:12px\" Author=meizz onclick=meizzDayClick(this.innerText)></td>');n++;}\r\n");
myString.Append("document.writeln('</tr>');}\r\n");
myString.Append("document.writeln(' <tr align=center><td width=20 height=20 style=\"font-size:12px\" id=meizzDay35 Author=meizz ');\r\n");
myString.Append("document.writeln(' onclick=meizzDayClick(this.innerText)></td>');\r\n");
myString.Append("document.writeln(' <td width=20 height=20 style=\"font-size:12px\" id=meizzDay36 Author=meizz onclick=meizzDayClick(this.innerText)></td></tr>');\r\n");
myString.Append("document.writeln(' </table></td></tr><tr><td>');\r\n");
myString.Append("document.writeln(' <table border=0 cellspacing=1 cellpadding=0 width=100% bgcolor=#FFFFFF>');\r\n");
myString.Append("document.writeln(' <tr><td Author=meizz align=left><input Author=meizz type=button value=\"<<\" title=\"向前翻 年\" onclick=\"meizzPrevY()\" ');\r\n");
myString.Append("document.writeln(' onfocus=\"this.blur()\" style=\"font-size: 12px; height: 20px\"><input Author=meizz title=\"向前翻 月\" type=button ');\r\n");
myString.Append("document.writeln(' value=\"<\" onclick=\"meizzPrevM()\" onfocus=\"this.blur()\" style=\"font-size: 12px; height: 20px\"></td><td ');\r\n");
myString.Append("document.writeln(' Author=meizz align=center><input Author=meizz type=button value=今天 onclick=\"meizzToday()\" ');\r\n");
myString.Append("document.writeln(' onfocus=\"this.blur()\" title=\"转到今天的日期\" style=\"font-size: 12px; height: 20px\"></td><td ');\r\n");
myString.Append("document.writeln(' Author=meizz align=right><input Author=meizz type=button value=\">\" onclick=\"meizzNextM()\" ');\r\n");
myString.Append("document.writeln(' onfocus=\"this.blur()\" title=\"往后翻 月\" style=\"font-size: 12px; height: 20px\"><input ');\r\n");
myString.Append("document.writeln(' Author=meizz type=button value=\">>\" title=\"往后翻 年\" onclick=\"meizzNextY()\"');\r\n");
myString.Append("document.writeln(' onfocus=\"this.blur()\" style=\"font-size: 12px; height: 20px\"></td>');\r\n");
myString.Append("document.writeln('</tr></table></td></tr></table></div>'); \r\n");
myString.Append("\r\n");
myString.Append("//==================================================== WEB 页面显示部分 ======================================================\r\n");
myString.Append("var outObject;\r\n");
myString.Append("function setday(tt,obj) //主调函数\r\n");
myString.Append("{\r\n");
myString.Append("if (arguments.length > 2){alert(\"对不起!传入本控件的参数太多!\");return;}\r\n");
myString.Append("if (arguments.length == 0){alert(\"对不起!您没有传回本控件任何参数!\");return;}\r\n");
myString.Append("var dads = document.all.meizzDateLayer.style;var th = tt;\r\n");
myString.Append("var ttop = tt.offsetTop; //TT控件的定位点高\r\n");
myString.Append("var thei = tt.clientHeight; //TT控件本身的高\r\n");
myString.Append("var tleft = tt.offsetLeft; //TT控件的定位点宽\r\n");
myString.Append("var ttyp = tt.type; //TT控件的类型\r\n");
myString.Append("while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}\r\n");
myString.Append("dads.top = (ttyp==\"image\")? ttop+thei : ttop+thei+6;\r\n");
myString.Append("dads.left = tleft;\r\n");
myString.Append("outObject = (arguments.length == 1) ? th : obj;\r\n");
myString.Append("dads.display = '';\r\n");
myString.Append("event.returnValue=false;\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("var MonHead = new Array(12); //定义阳历中每个月的最大天数\r\n");
myString.Append("MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4] = 31; MonHead[5] = 30;\r\n");
myString.Append("MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31; \r\n");
myString.Append("\r\n");
myString.Append("var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值\r\n");
myString.Append("var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值\r\n");
myString.Append("var meizzWDay=new Array(37); //定义写日期的数组 \r\n");
myString.Append("\r\n");
myString.Append("function document.onclick() //任意点击时关闭该控件\r\n");
myString.Append("{ \r\n");
myString.Append("with(window.event.srcElement)\r\n");
myString.Append("{ if (tagName != \"INPUT\" && getAttribute(\"Author\")==null)\r\n");
myString.Append("document.all.meizzDateLayer.style.display=\"none\";\r\n");
myString.Append("}\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function meizzWriteHead(yy,mm) //往 head 中写入当前的年与月\r\n");
myString.Append("{ document.all.meizzYearHead.innerText = yy;\r\n");
myString.Append("document.all.meizzMonthHead.innerText = mm;\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function tmpSelectYearInnerHTML(strYear) //年份的下拉框\r\n");
myString.Append("{\r\n");
myString.Append("if (strYear.match(/\\D/)!=null){alert(\"年份输入参数不是数字!\");return;}\r\n");
myString.Append("var m = (strYear) ? strYear : new Date().getFullYear();\r\n");
myString.Append("if (m < 1000 || m > 9999) {alert(\"年份值不在 1000 到 9999 之间!\");return;}\r\n");
myString.Append("var n = m - 10;\r\n");
myString.Append("if (n < 1000) n = 1000;\r\n");
myString.Append("if (n + 26 > 9999) n = 9974;\r\n");
myString.Append("var s = \"<select Author=meizz name=tmpSelectYear style='font-size: 12px' \"\r\n");
myString.Append("s += \"onblur='document.all.tmpSelectYearLayer.style.display=\\\"none\\\"' \"\r\n");
myString.Append("s += \"onchange='document.all.tmpSelectYearLayer.style.display=\\\"none\\\";\"\r\n");
myString.Append("s += \"meizzTheYear = this.value; meizzSetDay(meizzTheYear,meizzTheMonth)'>\\r\\n\";\r\n");
myString.Append("var selectInnerHTML = s;\r\n");
myString.Append("for (var i = n; i < n + 26; i++)\r\n");
myString.Append("{\r\n");
myString.Append("if (i == m)\r\n");
myString.Append("{selectInnerHTML += \"<option value='\" + i + \"' selected>\" + i + \"年\" + \"</option>\\r\\n\";}\r\n");
myString.Append("else {selectInnerHTML += \"<option value='\" + i + \"'>\" + i + \"年\" + \"</option>\\r\\n\";}\r\n");
myString.Append("}\r\n");
myString.Append("selectInnerHTML += \"</select>\";\r\n");
myString.Append("document.all.tmpSelectYearLayer.style.display=\"\";\r\n");
myString.Append("document.all.tmpSelectYearLayer.innerHTML = selectInnerHTML;\r\n");
myString.Append("document.all.tmpSelectYear.focus();\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框\r\n");
myString.Append("{\r\n");
myString.Append("if (strMonth.match(/\\D/)!=null){alert(\"月份输入参数不是数字!\");return;}\r\n");
myString.Append("var m = (strMonth) ? strMonth : new Date().getMonth() + 1;\r\n");
myString.Append("var s = \"<select Author=meizz name=tmpSelectMonth style='font-size: 12px' \"\r\n");
myString.Append("s += \"onblur='document.all.tmpSelectMonthLayer.style.display=\\\"none\\\"' \"\r\n");
myString.Append("s += \"onchange='document.all.tmpSelectMonthLayer.style.display=\\\"none\\\";\"\r\n");
myString.Append("s += \"meizzTheMonth = this.value; meizzSetDay(meizzTheYear,meizzTheMonth)'>\\r\\n\";\r\n");
myString.Append("var selectInnerHTML = s;\r\n");
myString.Append("for (var i = 1; i < 13; i++)\r\n");
myString.Append("{\r\n");
myString.Append("if (i == m)\r\n");
myString.Append("{selectInnerHTML += \"<option value='\"+i+\"' selected>\"+i+\"月\"+\"</option>\\r\\n\";}\r\n");
myString.Append("else {selectInnerHTML += \"<option value='\"+i+\"'>\"+i+\"月\"+\"</option>\\r\\n\";}\r\n");
myString.Append("}\r\n");
myString.Append("selectInnerHTML += \"</select>\";\r\n");
myString.Append("document.all.tmpSelectMonthLayer.style.display=\"\";\r\n");
myString.Append("document.all.tmpSelectMonthLayer.innerHTML = selectInnerHTML;\r\n");
myString.Append("document.all.tmpSelectMonth.focus();\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function closeLayer() //这个层的关闭\r\n");
myString.Append("{\r\n");
myString.Append("document.all.meizzDateLayer.style.display=\"none\";\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function document.onkeydown()\r\n");
myString.Append("{\r\n");
myString.Append("if (window.event.keyCode==27)document.all.meizzDateLayer.style.display=\"none\";\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function IsPinYear(year) //判断是否闰平年\r\n");
myString.Append("{\r\n");
myString.Append("if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function GetMonthCount(year,month) //闰年二月为29天\r\n");
myString.Append("{\r\n");
myString.Append("var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function GetDOW(day,month,year) //求某天的星期几\r\n");
myString.Append("{\r\n");
myString.Append("var dt=new Date(year,month-1,day).getDay()/7; return dt;\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function meizzPrevY() //往前翻 Year\r\n");
myString.Append("{\r\n");
myString.Append("if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}\r\n");
myString.Append("else{alert(\"年份超出范围(1000-9999)!\");}\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("}\r\n");
myString.Append("function meizzNextY() //往后翻 Year\r\n");
myString.Append("{\r\n");
myString.Append("if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}\r\n");
myString.Append("else{alert(\"年份超出范围(1000-9999)!\");}\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("}\r\n");
myString.Append("function meizzToday() //Today Button\r\n");
myString.Append("{\r\n");
myString.Append("meizzTheYear = new Date().getFullYear();\r\n");
myString.Append("meizzTheMonth = new Date().getMonth()+1;\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("}\r\n");
myString.Append("function meizzPrevM() //往前翻月份\r\n");
myString.Append("{\r\n");
myString.Append("if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("}\r\n");
myString.Append("function meizzNextM() //往后翻月份\r\n");
myString.Append("{\r\n");
myString.Append("if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("} \r\n");
myString.Append("\r\n");
myString.Append("function meizzSetDay(yy,mm) //主要的写程序**********\r\n");
myString.Append("{\r\n");
myString.Append("meizzWriteHead(yy,mm);\r\n");
myString.Append("for (var i = 0; i < 37; i++){meizzWDay[i]=\"\"}; //将显示框的内容全部清空\r\n");
myString.Append("var day1 = 1,firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期几\r\n");
myString.Append("for (var i = firstday; day1 < GetMonthCount(yy,mm)+1; i++){meizzWDay[i]=day1;day1++;}\r\n");
myString.Append("for (var i = 0; i < 37; i++)\r\n");
myString.Append("{ var da = eval(\"document.all.meizzDay\"+i) //书写新的一个月的日期星期排列\r\n");
myString.Append("if (meizzWDay[i]!=\"\")\r\n");
myString.Append("{ da.innerHTML = \"<b>\" + meizzWDay[i] + \"</b>\";\r\n");
myString.Append("da.style.backgroundColor = (yy == new Date().getFullYear() &&\r\n");
myString.Append("mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ? \"#FFD700\" : \"#ADD8E6\";\r\n");
myString.Append("da.style.cursor=\"hand\"\r\n");
myString.Append("}\r\n");
myString.Append("else{da.innerHTML=\"\";da.style.backgroundColor=\"\";da.style.cursor=\"default\"}\r\n");
myString.Append("}\r\n");
myString.Append("}\r\n");
myString.Append("function meizzDayClick(n) //点击显示框选取日期,主输入函数*************\r\n");
myString.Append("{\r\n");
myString.Append("var yy = meizzTheYear;\r\n");
myString.Append("var mm = meizzTheMonth;\r\n");
myString.Append("if (mm < 10){mm = \"0\" + mm;}\r\n");
myString.Append("if (outObject)\r\n");
myString.Append("{\r\n");
myString.Append("if (!n) {outObject.value=\"\"; return;}\r\n");
myString.Append("if ( n < 10){n = \"0\" + n;}\r\n");
myString.Append("outObject.value= yy + \"-\" + mm + \"-\" + n ; //注:在这里你可以输出改成你想要的格式\r\n");
myString.Append("closeLayer(); \r\n");
myString.Append("}\r\n");
myString.Append("else {closeLayer(); alert(\"您所要输出的控件对象并不存在!\");}\r\n");
myString.Append("}\r\n");
myString.Append("meizzSetDay(meizzTheYear,meizzTheMonth);\r\n");
myString.Append("// -->\r\n");
myString.Append("</script>\r\n");
return myString.ToString();
}
}
}