您的位置:首页 > 编程语言 > ASP

asp.net自定义日历控件,兼容Asp.net ajax

2006-12-22 14:31 489 查看
Asp.net Ajax版本:


using System;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.ComponentModel;


using System.Drawing;


using System.Web;


using System.Text;


namespace SmartCalendar




...{
/// <summary>


/// 名称:SmartCalendar


/// 版本:2.0(该版本支持Asp.net Ajax,在Asp.net Ajax RC1版本测试通过)


/// 作者:小青青


/// @CopyRight:2006 不得用于商业用途


/// </summary>


[DefaultProperty("Value"),


ToolboxData("<{0}:SmartCalendar runat=server></{0}:SmartCalendar>")]


public class SmartCalendar : WebControl, INamingContainer




...{


TextBox txtValue = new TextBox();


CalendarFormat _DateFormat;




protected
4000
override void OnInit(EventArgs e)




...{




this.Controls.Add(txtValue);


if(ReadOnly)


txtValue.Attributes.Add("ReadOnly", string.Empty);


txtValue.Style.Add(HtmlTextWriterStyle.FontFamily, "arial");


txtValue.Style.Add(HtmlTextWriterStyle.FontSize, "11px");


txtValue.Style.Add(HtmlTextWriterStyle.Width, "82px");


if (!Page.IsPostBack)


Value = DateTime.Now;


}






public enum CalendarFormat ...{ Chinese, Standard }




/**//// <summary>


/// 设置或获取控件是否为只读


/// </summary>


[DefaultValue(true), Bindable(true), Themeable(true)]


public virtual bool ReadOnly




...{


get




...{


object obj1 = this.ViewState["ReadOnly"];


if (obj1 != null)




...{


return (bool)obj1;


}


return true;


}


set




&
24000
nbsp; ...{


this.ViewState["ReadOnly"] = value;


}


}








/**//// <summary>


/// 设置或获取控件是否可用


/// </summary>


[DefaultValue(true), Bindable(true), Themeable(false)]


public override bool Enabled




...{


get




...{


return base.Enabled;


}


set




...{


base.Enabled = value;




txtValue.Enabled = value;




}


}








/**//// <summary>


/// 设置或获取控件的日期格式


/// </summary>


[DefaultValue(IPTO.Web.SmartCalendar.CalendarFormat.Chinese), Themeable(true),Bindable(true)]


public CalendarFormat DateFormat




...{




get




...{




return _DateFormat;




}


set




...{




_DateFormat = value;




}




}




/**//// <summary>


/// 获取或设置控件的日期值,如果日期不符合规则,将默认为当天


/// </summary>


public DateTime Value




...{






get ...{




try




...{


string tmp = txtValue.Text;




if (!string.IsNullOrEmpty(tmp))




...{


tmp = tmp.Replace("年", "-");


tmp = tmp.Replace("月", "-");


tmp = tmp.Replace("日", "-");


}






string[] strList = tmp.Split(new string[]...{"-"}, StringSplitOptions.RemoveEmptyEntries);




if(strList.Length == 3)


return new DateTime(int.Parse(strList[0]), int.Parse(strList[1]), int.Parse(strList[2]));


else


return DateTime.Now;




}


catch




...{




return DateTime.Now;




}




}




set ...{




if (DateFormat == CalendarFormat.Chinese)




...{


txtValue.Text = value.Year.ToString() + "年";


txtValue.Text += value.Month.ToString() + "月";


txtValue.Text += value.Day.ToString() + "日";


}


else




...{




txtValue.Text = value.Year.ToString() + "-";


txtValue.Text += value.Month.ToString() + "-";


txtValue.Text += value.Day.ToString() + "-";




}




}




}




protected override void Render(HtmlTextWriter writer)




...{


StringBuilder sb = new StringBuilder();


writer.Write("<div id='" + this.ClientID + "div' align='left' style='z-index:999;width:109px;'>");


txtValue.RenderControl(writer);




sb.Append("<input UNSELECTABLE type='button' HIDEFOCUS value='i' style='font-family:webdings;height:20px;font-size:13px;cursor:hand' onclick="if(" + this.ClientID + "cal.style.display!='none')" + this.ClientID + "cal.style.display='none';else " + this.ClientID + "cal.style.display='';"><BR/></div>");




writer.Write(sb.ToString());








}




public string InitializeScript




...{




get




...{


StringBuilder sb = new StringBuilder();


sb.Append("<script>");


sb.Append("try{var htm1 ="<div UNSELECTABLE id='" + this.ClientID + "cal' style='border:solid 1px #35A2E5;display:none; width:200px;position:absolute;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5,Color=#aaaaaa, Positive=true);'>"; ");


sb.Append("htm1+="<IFRAME width=200 height=43 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>"; ");


sb.Append("htm1+="<div UNSELECTABLE style='background-color:#35A2E5;height:32px;padding:10px 5px 0px 12px'>"; ");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='PreMonth(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='上一月' style='cursor:hand;font-size:12px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▲</LABEL>"; ");


sb.Append("htm1+="<SELECT UNSELECTABLE ID='" + this.ClientID + "ddlMonth' style='font-size:12px;' onchange='monthCur=this.options[this.selectedIndex].value-1;RenderDate(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);'/></SELECT>"; ");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='NextMonth(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='下一月' style='cursor:hand;font-size:13px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▼</LABEL>"; ");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='PreYear(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='上一年' style='cursor:hand;font-size:12px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▲</LABEL>"; ");


sb.Append("htm1+="<SELECT UNSELECTABLE ID='" + this.ClientID + "ddlYear' style='font-size:12px;' onchange='yearCur=this.options[this.selectedIndex].value-1;RenderDate(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlYear'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);'/></SELECT>"; ");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='NextYear(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='下一月' style='cursor:hand;font-size:13px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▼</LABEL>"; ");


sb.Append("htm1+="</div><div id='" + this.ClientID + "main'></div></div></div>"; ");


sb.Append("document.all['" + this.ClientID + "div'].innerHTML+=htm1;}catch(e){}</script> ");


sb.Append("<script>try{yearCur=" + Value.Year.ToString() + ";monthCur=" + (Value.Month - 1).ToString() + ";init(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);}catch(e){}</script> ");




return sb.ToString();




}




}




protected override void OnPreRender(EventArgs e)




...{




StringBuilder sb = new StringBuilder("var today=new Date(); ");


sb.Append("var dateNow=today.getDate(); ");


sb.Append("var monthNow=today.getMonth(); ");


sb.Append("var yearNow=today.getYear(); ");


sb.Append("var dateCur=today.getDate(); ");


sb.Append("var monthCur=today.getMonth(); ");


sb.Append("var yearCur=today.getYear(); ");


sb.Append("var yearMax=2050; ");


sb.Append("var yearMin=1990; ");


sb.Append("var months= new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); ");


sb.Append("var weeks= new Array("星期一","星期二","星期三","星期四","星期五","星期六","星期日"); ");


sb.Append(" ");


//initialize method


sb.Append("function init(oy,om,ovalue,omain){ ");


sb.Append(" for(var i=yearMin;i<=yearMax;i++){ ");


sb.Append(" var oOption = document.createElement("OPTION"); ");


sb.Append(" oOption.text=i; ");


sb.Append(" oOption.value=i; ");


sb.Append(" oy.add(oOption); ");


sb.Append(" } ");


sb.Append(" for(var i=0;i<months.length;i++){ ");


sb.Append(" var oOption = document.createElement("OPTION"); ");


sb.Append(" oOption.text=months[i]; ");


sb.Append(" oOption.value=i + 1; ");


sb.Append(" om.add(oOption); ");


sb.Append(" } ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append("} ");


sb.Append("function RenderDate(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" var y=yearCur; ");


sb.Append(" var m=monthCur; ");


sb.Append(" var d=dateCur; ");


sb.Append(" var dayfirst=new Date(y,m,1).getDay();//每个月的第一天星期几 ");


sb.Append(" var datefirst=1-dayfirst;//每个月的第一个日期,应为第一天减去第一天的星期几 ");


sb.Append(" var TABLE_B="<TABLE UNSELECTABLE width='200px' cellspacing=0 cellpadding=2 style='background:#F4FBFE;border-collapse: separate;text-align:center;font-size:11px;font-family:arial'>"; ");


sb.Append(" var TABLE_B1="<TABLE UNSELECTABLE width='200px' cellspacing=0 cellpadding=1 style='background:#F4FBFE;border-collapse: separate;text-align:center;font-size:11px;font-family:arial;border-top:solid 2px #35A2E5;'>"; ");


sb.Append(" var TR_FIRST="<TR><TD><B>日</B></TD><TD><B>一</B></TD><TD><B>二</B></TD><TD><B>三</B></TD><TD><B>四</B></TD><TD><B>五</B></TD><TD><B>六</B></TD></TR><TR height='2px'>"; ");


sb.Append(" var EMPTY_CELL="<TD> </TD>"; ");


sb.Append(" var TD_B="<TD style='cursor:hand;' onmouseover=/"this.style.background='#D8EAFC'/" onmouseout=/"this.style.background='#F4FBFE'/" "; ");


sb.Append(" var TD_E="</TD>"; ");


sb.Append(" var TR_B="<TR>"; ");


sb.Append(" var TR_E="</TR>"; ");


sb.Append(" var TABLE_E="</TABLE>" ");


sb.Append(" var LINK_DATE_M="/">" ");


sb.Append(" var LINK_DATE_E="</A>"; ");


sb.Append(" var IRM1="<IFRAME width=200 height=130 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>"; ");


sb.Append(" var IRM2="<IFRAME width=200 height=147 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>"; ");


sb.Append(" var htm; ");


sb.Append(" htm = TABLE_B; ");


sb.Append(" htm += TR_FIRST; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" htm += TABLE_B1; ");


sb.Append(" //begin ");


sb.Append(" var count=ComputeDateCount(y,m);var rCount=0; ");


sb.Append(" while(datefirst<=count) ");


sb.Append(" { ");


sb.Append(" rCount++; ");


sb.Append(" htm += TR_B; ");


sb.Append(" for(var i=1;i<=7;i++) ");


sb.Append(" { ");


sb.Append(" if (datefirst<=0) ");


sb.Append(" htm += EMPTY_CELL; ");


sb.Append(" else if (datefirst>count) ");


sb.Append(" htm += EMPTY_CELL; ");


sb.Append(" else ");




if (DateFormat == CalendarFormat.Chinese)




...{


sb.Append(" if (yearCur==yearNow && monthCur==monthNow && dateCur==datefirst) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<B><A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E + "</B>"; ");


sb.Append(" else ");


sb.Append(" if (i==7) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;color:green' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else if(i==1) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;color:red' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E; ");


}


else




...{


sb.Append(" if (yearCur==yearNow && monthCur==monthNow && dateCur==datefirst) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<B><A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E + "</B>"; ");


sb.Append(" else ");


sb.Append(" if (i==7) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;color:green' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else if(i==1) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;color:red' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E; ");


}


sb.Append(" datefirst++; ");


sb.Append(" } ");


sb.Append(" } ");


sb.Append(" htm += TR_E; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" htm += TABLE_B; ");


sb.Append(" htm += TR_B + "<TD colspan='7'><input style='height:100%;width:100%;cursor:hand;background:#D8EAFC;border:solid 1px #35A2E5;font-family:arial;font-size:12px' HIDEFOCUS type='button' onclick='SetToday(/""+oy.id+"/",/""+om.id+"/",/""+ovalue+"/",/""+omain.id+"/")' value='今天:"+yearNow+'年'+(monthNow+1)+'月'+dateNow+'日' + weeks[new Date().getDay()-1] +"'></TD>" + TR_E; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" if(rCount==5) ");


sb.Append(" htm = IRM1 + htm; ");


sb.Append(" else ");


sb.Append(" htm = IRM2 + htm; ");


sb.Append(" omain.innerHTML = htm; ");


sb.Append("} ");


sb.Append("function SetToday(oy,om,ovalue,omain) ");


sb.Append("{ ");


if (DateFormat == CalendarFormat.Chinese)


sb.Append(" document.all[ovalue].value=yearNow+'年'+(monthNow+1)+'月'+dateNow+'日'; ");


else


sb.Append(" document.all[ovalue].value=yearNow+'-'+(monthNow+1)+'-'+dateNow; ");


sb.Append(" yearCur=yearNow; ");


sb.Append(" monthCur=monthNow; ");


sb.Append(" dateCur=dateNow; ");


sb.Append(" SetDropDown(yearNow,monthNow,document.all[oy],document.all[om],ovalue,document.all[omain]); ");


sb.Append("} ");


sb.Append("function PreYear(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (yearCur>yearMin) ");


sb.Append(" { ");


sb.Append(" yearCur--; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function NextYear(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (yearCur<yearMax) ");


sb.Append(" { ");


sb.Append(" yearCur++; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function PreMonth(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (monthCur>0) ");


sb.Append(" { ");


sb.Append(" monthCur--; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append(" } ");


sb.Append("function NextMonth(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (monthCur<11) ");


sb.Append(" { ");


sb.Append(" monthCur++; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function SetDropDown(y,m,oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" for(var i=0;i<oy.options.length;i++) ");


sb.Append(" { ");


sb.Append(" if(oy.options[i].value==y) ");


sb.Append(" oy.options[i].selected=true; ");


sb.Append(" } ");


sb.Append(" for(var i=0;i<om.options.length;i++) ");


sb.Append(" { ");


sb.Append(" if(om.options[i].value==m+1) ");


sb.Append(" om.options[i].selected=true; ");


sb.Append(" } ");


sb.Append(" RenderDate(oy,om,ovalue,omain); ");


sb.Append("} ");


sb.Append("function ComputeDateCount(y,m) ");


sb.Append("{ ");


sb.Append(" var count; ");


sb.Append(" switch (parseInt(m+1)) ");


sb.Append(" { ");


sb.Append(" case 1: ");


sb.Append(" case 3: ");


sb.Append(" case 5: ");


sb.Append(" case 7: ");


sb.Append(" case 8: ");


sb.Append(" case 10: ");


sb.Append(" case 12: ");


sb.Append(" count=31; ");


sb.Append(" break; ");


sb.Append(" case 2: ");


sb.Append(" if(IsLeapYear(y)) ");


sb.Append(" { ");


sb.Append(" count=29; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" else ");


sb.Append(" { ");


sb.Append(" count=28; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" default: ");


sb.Append(" count=30; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" return count; ");


sb.Append("} ");


sb.Append("function IsLeapYear(y) ");


sb.Append("{ ");


sb.Append(" if (y%400==0||((y%4)==0&&(y%100)!=0)) ");


sb.Append(" return true; ");


sb.Append(" return false; ");


sb.Append("} ");






if (!Page.ClientScript.IsClientScriptBlockRegistered("Calendar"))


Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "Calendar", sb.ToString(), true);






base.OnPreRender(e);


}






}


}

由于Asp.net Ajax对于后台输出Javascript有较大限制,所以请注意以下几点:

在Page_Load方法中加入以下代码:(请注意要将该段代码放在IsPostBack的判断语句之外)


//smartCalendar为控件实例


//updatePanel为Asp.net AjaxUpdatePanel控件


//以下为Asp.net Ajax RC1版本代码


System.Web.UI.ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), smartCalendar.UniqueID, smartCalendar.InitializeScript, false);

如果页面中使用了类似于MutiView、View的控件,请确保第一个页面中一定有SmartCalendar控件,否则会有Javascript异常;如果第一页中不需要日历控件也请将其拖拽到页面上,可以通过将其加入到一个Style="display:none"的Div中,来实现日历控件的隐藏。

非Asp.net Ajax版本:


using System;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.ComponentModel;


using System.Drawing;


using System.Web;


using System.Text;


namespace IPTO.Web




...{




/**//// <summary>


/// 名称:SmartCalendar


/// 版本:2.0(该版本不支持Asp.net Ajax)


/// 作者:小青青


/// @CopyRight: 2006 不得用于商业用途


/// </summary>


[DefaultProperty("Value"),


ToolboxData("<{0}:SmartCalendar runat=server></{0}:SmartCalendar>")]


public class SmartCalendar : WebControl, INamingContainer




...{


TextBox txtValue = new TextBox();


CalendarFormat _DateFormat;


protected override void OnInit(EventArgs e)




...{




this.Controls.Add(txtValue);


if(ReadOnly)


txtValue.Attributes.Add("ReadOnly", string.Empty);


txtValue.Style.Add(HtmlTextWriterStyle.FontFamily, "arial");


txtValue.Style.Add(HtmlTextWriterStyle.FontSize, "11px");


txtValue.Style.Add(HtmlTextWriterStyle.Width, "82px");


if (!Page.IsPostBack)


Value = DateTime.Now;


}






public enum CalendarFormat ...{ Chinese, Standard }




/**//// <summary>


/// 设置或获取控件是否为只读


/// </summary>


[DefaultValue(true), Bindable(true), Themeable(true)]


public virtual bool ReadOnly




...{


get




...{


object obj1 = this.ViewState["ReadOnly"];


if (obj1 != null)




...{


return (bool)obj1;


}


return true;


}


set




...{


this.ViewState["ReadOnly"] = value;


}


}








/**//// <summary>


/// 设置或获取控件是否可用


/// </summary>


[DefaultValue(true), Bindable(true), Themeable(false)]


public override bool Enabled




...{


get




...{


return base.Enabled;


}


set




...{


base.Enabled = value;




txtValue.Enabled = value;




}


}








/**//// <summary>


/// 设置或获取控件的日期格式


/// </summary>


[DefaultValue(IPTO.Web.SmartCalendar.CalendarFormat.Chinese), Themeable(true),Bindable(true)]


public CalendarFormat DateFormat




...{




get




...{




return _DateFormat;




}


set




...{




_DateFormat = value;




}




}




/**//// <summary>


/// 获取或设置控件的日期值,如果日期不符合规则,将默认为当天


/// </summary>


public DateTime Value




...{






get ...{




try




...{


string tmp = txtValue.Text;




if (!string.IsNullOrEmpty(tmp))




...{


tmp = tmp.Replace("年", "-");


tmp = tmp.Replace("月", "-");


tmp = tmp.Replace("日", "-");


}






string[] strList = tmp.Split(new string[]...{"-"}, StringSplitOptions.RemoveEmptyEntries);




if(strList.Length == 3)


return new DateTime(int.Parse(strList[0]), int.Parse(strList[1]), int.Parse(strList[2]));


else


return DateTime.Now;




}


catch




...{




return DateTime.Now;




}




}




set ...{




if (DateFormat == CalendarFormat.Chinese)




...{


txtValue.Text = value.Year.ToString() + "年";


txtValue.Text += value.Month.ToString() + "月";


txtValue.Text += value.Day.ToString() + "日";


}


else




...{




txtValue.Text = value.Year.ToString() + "-";


txtValue.Text += value.Month.ToString() + "-";


txtValue.Text += value.Day.ToString() + "-";




}




}




}




protected override void Render(HtmlTextWriter writer)




...{


StringBuilder sb = new StringBuilder();


writer.Write("<div id='" + this.ClientID + "div' align='left' style='z-index:999;width:109px;'>");


txtValue.RenderControl(writer);




sb.Append("<input UNSELECTABLE type='button' HIDEFOCUS value='i' style='font-family:webdings;height:20px;font-size:13px;cursor:hand' onclick="if(" + this.ClientID + "cal.style.display!='none')" + this.ClientID + "cal.style.display='none';else " + this.ClientID + "cal.style.display='';"><BR/>");


sb.Append("<script>");


sb.Append("var htm1 ="<div UNSELECTABLE id='" + this.ClientID + "cal' style='border:solid 1px #35A2E5;display:none; width:200px;position:absolute;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5,Color=#aaaaaa, Positive=true);'>";");


sb.Append("htm1+="<IFRAME width=200 height=43 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>";");


sb.Append("htm1+="<div UNSELECTABLE style='background-color:#35A2E5;height:32px;padding:10px 5px 0px 12px'>";");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='PreMonth(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='上一月' style='cursor:hand;font-size:12px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▲</LABEL>";");


sb.Append("htm1+="<SELECT UNSELECTABLE ID='" + this.ClientID + "ddlMonth' style='font-size:12px;' onchange='monthCur=this.options[this.selectedIndex].value-1;RenderDate(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);'/></SELECT>";");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='NextMonth(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='下一月' style='cursor:hand;font-size:13px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▼</LABEL>";");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='PreYear(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='上一年' style='cursor:hand;font-size:12px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▲</LABEL>";");


sb.Append("htm1+="<SELECT UNSELECTABLE ID='" + this.ClientID + "ddlYear' style='font-size:12px;' onchange='yearCur=this.options[this.selectedIndex].value-1;RenderDate(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlYear'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);'/></SELECT>";");


sb.Append("htm1+="<LABEL UNSELECTABLE onmousedown='NextYear(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main'])' title='下一月' style='cursor:hand;font-size:13px' onmouseover='this.style.color='white'' onmouseout='this.style.color='''>▼</LABEL>";");


sb.Append("htm1+="</div><div id='" + this.ClientID + "main'></div></div></div>";");


sb.Append("document.all['" + this.ClientID + "div'].innerHTML+=htm1;</script>");


sb.Append("<script>yearCur=" + Value.Year.ToString() + ";monthCur=" + (Value.Month - 1).ToString() + ";init(document.all['" + this.ClientID + "ddlYear'], document.all['" + this.ClientID + "ddlMonth'],'" + txtValue.UniqueID + "', document.all['" + this.ClientID + "main']);</script>");




writer.Write(sb.ToString());




}


protected override void OnPreRender(EventArgs e)




...{




StringBuilder sb = new StringBuilder("var today=new Date(); ");


sb.Append("var dateNow=today.getDate(); ");


sb.Append("var monthNow=today.getMonth(); ");


sb.Append("var yearNow=today.getYear(); ");


sb.Append("var dateCur=today.getDate(); ");


sb.Append("var monthCur=today.getMonth(); ");


sb.Append("var yearCur=today.getYear(); ");


sb.Append("var yearMax=2050; ");


sb.Append("var yearMin=1990; ");


sb.Append("var months= new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); ");


sb.Append("var weeks= new Array("星期一","星期二","星期三","星期四","星期五","星期六","星期日"); ");


sb.Append(" ");


//initialize method


sb.Append("function init(oy,om,ovalue,omain){ ");


sb.Append(" for(var i=yearMin;i<=yearMax;i++){ ");


sb.Append(" var oOption = document.createElement("OPTION"); ");


sb.Append(" oOption.text=i; ");


sb.Append(" oOption.value=i; ");


sb.Append(" oy.add(oOption); ");


sb.Append(" } ");


sb.Append(" for(var i=0;i<months.length;i++){ ");


sb.Append(" var oOption = document.createElement("OPTION"); ");


sb.Append(" oOption.text=months[i]; ");


sb.Append(" oOption.value=i + 1; ");


sb.Append(" om.add(oOption); ");


sb.Append(" } ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append("} ");


sb.Append("function RenderDate(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" var y=yearCur; ");


sb.Append(" var m=monthCur; ");


sb.Append(" var d=dateCur; ");


sb.Append(" var dayfirst=new Date(y,m,1).getDay();//每个月的第一天星期几 ");


sb.Append(" var datefirst=1-dayfirst;//每个月的第一个日期,应为第一天减去第一天的星期几 ");


sb.Append(" var TABLE_B="<TABLE UNSELECTABLE width='200px' cellspacing=0 cellpadding=2 style='background:#F4FBFE;border-collapse: separate;text-align:center;font-size:11px;font-family:arial'>"; ");


sb.Append(" var TABLE_B1="<TABLE UNSELECTABLE width='200px' cellspacing=0 cellpadding=1 style='background:#F4FBFE;border-collapse: separate;text-align:center;font-size:11px;font-family:arial;border-top:solid 2px #35A2E5;'>"; ");


sb.Append(" var TR_FIRST="<TR><TD><B>日</B></TD><TD><B>一</B></TD><TD><B>二</B></TD><TD><B>三</B></TD><TD><B>四</B></TD><TD><B>五</B></TD><TD><B>六</B></TD></TR><TR height='2px'>"; ");


sb.Append(" var EMPTY_CELL="<TD> </TD>"; ");


sb.Append(" var TD_B="<TD style='cursor:hand;' onmouseover=/"this.style.background='#D8EAFC'/" onmouseout=/"this.style.background='#F4FBFE'/" "; ");


sb.Append(" var TD_E="</TD>"; ");


sb.Append(" var TR_B="<TR>"; ");


sb.Append(" var TR_E="</TR>"; ");


sb.Append(" var TABLE_E="</TABLE>" ");


sb.Append(" var LINK_DATE_M="/">" ");


sb.Append(" var LINK_DATE_E="</A>"; ");


sb.Append(" var IRM1="<IFRAME width=200 height=130 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>"; ");


sb.Append(" var IRM2="<IFRAME width=200 height=147 border=0 style='position:absolute; z-index:-1; display:block'></IFRAME>"; ");


sb.Append(" var htm; ");


sb.Append(" htm = TABLE_B; ");


sb.Append(" htm += TR_FIRST; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" htm += TABLE_B1; ");


sb.Append(" //begin ");


sb.Append(" var count=ComputeDateCount(y,m);var rCount=0; ");


sb.Append(" while(datefirst<=count) ");


sb.Append(" { ");


sb.Append(" rCount++; ");


sb.Append(" htm += TR_B; ");


sb.Append(" for(var i=1;i<=7;i++) ");


sb.Append(" { ");


sb.Append(" if (datefirst<=0) ");


sb.Append(" htm += EMPTY_CELL; ");


sb.Append(" else if (datefirst>count) ");


sb.Append(" htm += EMPTY_CELL; ");


sb.Append(" else ");




if (DateFormat == CalendarFormat.Chinese)




...{


sb.Append(" if (yearCur==yearNow && monthCur==monthNow && dateCur==datefirst) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<B><A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E + "</B>"; ");


sb.Append(" else ");


sb.Append(" if (i==7) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;color:green' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else if(i==1) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;color:red' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "年" + (m+1) + "月" + datefirst + "日'/">" + "<A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E; ");


}


else




...{


sb.Append(" if (yearCur==yearNow && monthCur==monthNow && dateCur==datefirst) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<B><A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E + "</B>"; ");


sb.Append(" else ");


sb.Append(" if (i==7) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;color:green' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else if(i==1) ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;color:red' >" + datefirst + LINK_DATE_E + TD_E; ");


sb.Append(" else ");


sb.Append(" htm += TD_B + "onclick=/"dateCur="+datefirst+";document.all['"+ovalue+"'].value='" + y + "-" + (m+1) + "-" + datefirst + "'/">" + "<A style='cursor:hand;' >" + datefirst + LINK_DATE_E + TD_E; ");


}


sb.Append(" datefirst++; ");


sb.Append(" } ");


sb.Append(" } ");


sb.Append(" htm += TR_E; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" htm += TABLE_B; ");


sb.Append(" htm += TR_B + "<TD colspan='7'><input style='height:100%;width:100%;cursor:hand;background:#D8EAFC;border:solid 1px #35A2E5;font-family:arial;font-size:12px' HIDEFOCUS type='button' onclick='SetToday(/""+oy.id+"/",/""+om.id+"/",/""+ovalue+"/",/""+omain.id+"/")' value='今天:"+yearNow+'年'+(monthNow+1)+'月'+dateNow+'日' + weeks[new Date().getDay()-1] +"'></TD>" + TR_E; ");


sb.Append(" htm += TABLE_E; ");


sb.Append(" if(rCount==5) ");


sb.Append(" htm = IRM1 + htm; ");


sb.Append(" else ");


sb.Append(" htm = IRM2 + htm; ");


sb.Append(" omain.innerHTML = htm; ");


sb.Append("} ");


sb.Append("function SetToday(oy,om,ovalue,omain) ");


sb.Append("{ ");


if (DateFormat == CalendarFormat.Chinese)


sb.Append(" document.all[ovalue].value=yearNow+'年'+(monthNow+1)+'月'+dateNow+'日'; ");


else


sb.Append(" document.all[ovalue].value=yearNow+'-'+(monthNow+1)+'-'+dateNow; ");


sb.Append(" yearCur=yearNow; ");


sb.Append(" monthCur=monthNow; ");


sb.Append(" dateCur=dateNow; ");


sb.Append(" SetDropDown(yearNow,monthNow,document.all[oy],document.all[om],ovalue,document.all[omain]); ");


sb.Append("} ");


sb.Append("function PreYear(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (yearCur>yearMin) ");


sb.Append(" { ");


sb.Append(" yearCur--; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function NextYear(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (yearCur<yearMax) ");


sb.Append(" { ");


sb.Append(" yearCur++; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function PreMonth(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (monthCur>0) ");


sb.Append(" { ");


sb.Append(" monthCur--; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append(" } ");


sb.Append("function NextMonth(oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" if (monthCur<11) ");


sb.Append(" { ");


sb.Append(" monthCur++; ");


sb.Append(" SetDropDown(yearCur,monthCur,oy,om,ovalue,omain); ");


sb.Append(" } ");


sb.Append("} ");


sb.Append("function SetDropDown(y,m,oy,om,ovalue,omain) ");


sb.Append("{ ");


sb.Append(" for(var i=0;i<oy.options.length;i++) ");


sb.Append(" { ");


sb.Append(" if(oy.options[i].value==y) ");


sb.Append(" oy.options[i].selected=true; ");


sb.Append(" } ");


sb.Append(" for(var i=0;i<om.options.length;i++) ");


sb.Append(" { ");


sb.Append(" if(om.options[i].value==m+1) ");


sb.Append(" om.options[i].selected=true; ");


sb.Append(" } ");


sb.Append(" RenderDate(oy,om,ovalue,omain); ");


sb.Append("} ");


sb.Append("function ComputeDateCount(y,m) ");


sb.Append("{ ");


sb.Append(" var count; ");


sb.Append(" switch (parseInt(m+1)) ");


sb.Append(" { ");


sb.Append(" case 1: ");


sb.Append(" case 3: ");


sb.Append(" case 5: ");


sb.Append(" case 7: ");


sb.Append(" case 8: ");


sb.Append(" case 10: ");


sb.Append(" case 12: ");


sb.Append(" count=31; ");


sb.Append(" break; ");


sb.Append(" case 2: ");


sb.Append(" if(IsLeapYear(y)) ");


sb.Append(" { ");


sb.Append(" count=29; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" else ");


sb.Append(" { ");


sb.Append(" count=28; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" default: ");


sb.Append(" count=30; ");


sb.Append(" break; ");


sb.Append(" } ");


sb.Append(" return count; ");


sb.Append("} ");


sb.Append("function IsLeapYear(y) ");


sb.Append("{ ");


sb.Append(" if (y%400==0||((y%4)==0&&(y%100)!=0)) ");


sb.Append(" return true; ");


sb.Append(" return false; ");


sb.Append("} ");






if (!Page.ClientScript.IsClientScriptBlockRegistered("Calendar"))


Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "Calendar", sb.ToString(), true);






base.OnPreRender(e);


}






}


}



http://blog.csdn.net/justinalucard/archive/2006/12/22/1453372.aspx
转载请注明出处,即本贴地址及作者!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息