YearMonthDayDownDropList.ascx:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="YearMonthDayDownDropList.ascx.cs"
Inherits="micrm.Modules.YearMonthDayDownDropList"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%> <table id="Table1" cellSpacing="0" cellPadding="0"> <tr> <td><SELECT id=<%=YearName%> onclick=<%=JavascriptFunName%>
style="WIDTH: 53px; HEIGHT: 61px" name=<%=YearName%>> <% FillOptions(2000,ServerNowYear,ServerYear);%> </SELECT> <td> <DIV>年</DIV> </td> <td><SELECT id=<%=MonthName%> style="WIDTH: 45px; HEIGHT:
61px" onclick=<%=JavascriptFunName%> name=<%=MonthName%>> <% FillOptions(1,12,ServerMonth);%> </SELECT> <td> <DIV>月</DIV> </td> <td><SELECT id=<%=DayName%> style="WIDTH: 45px; HEIGHT:
61px" name=<%=DayName%>> <% FillOptions(1,ServerMonthDays,ServerDay); %> </SELECT></td> <td> <DIV>日</DIV> </td> </tr> </table>
在YearMonthDayDownDropList.ascx.cs中:
namespace micrm.Modules { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
/// <summary> /// YearMonthDayDownDropList 的摘要说明。 /// </summary> public abstract class YearMonthDayDownDropList : System.Web.UI.UserControl {
//选择的年月日如:20021225 public string YearMonthDay { get { return Request.Form[YearName]+Request.Form[MonthName]+Request.Form[DayName]; } } protected int ServerYear; //服务器当前选择年 protected int ServerMonth;//服务器当前月 protected int ServerNowYear; //服务器当前年 protected int ServerDay; //服务器当前天 protected int ServerMonthDays;//当前月天数 protected string JavascriptFunName; //此user control发出的函数名称 protected string YearName; //此user control发出的年控件的名称 protected string MonthName;//此user control发出的月控件的名称 protected string DayName; //此user control发出的日控件的名称 private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string id= this.UniqueID; if(!this.Page.IsClientScriptBlockRegistered(id)) { JavascriptFunName="chanday"+id+"()"; YearName="year"+id; MonthName="month"+id; DayName="day"+id; string scriptString ="<script language=javascript>"; scriptString=scriptString+"function "+JavascriptFunName; scriptString=scriptString+ "{ var days;"; scriptString=scriptString+" var currentyear;"; scriptString=scriptString +"days=31;"; scriptString=scriptString+" if(window.document.forms[0]."+MonthName+".value==04||window.document.forms[0]."+MonthName+".value==06||window.document.forms[0]."+MonthName+".value==09||window.document.forms[0]."+MonthName+".value==11)"; scriptString=scriptString+" days=30;"; scriptString=scriptString+"else if(window.document.forms[0]."+MonthName+".value==02) {"; scriptString=scriptString+"Nowyear=window.document.forms[0]."+YearName+".value ;"; scriptString=scriptString+ " if((Nowyear%4==0 &&Nowyear%100!=0) || Nowyear%400==0)"; scriptString=scriptString+" days=29;"; scriptString=scriptString+" else days=28;"; scriptString=scriptString+" }"; scriptString=scriptString+ " flen=window.document.forms[0]."+DayName+".length ;"; scriptString=scriptString+" window.document.forms[0]."+DayName+".length =days;"; scriptString=scriptString+ " i=flen+1;"; scriptString=scriptString+"for(i;i<=days;i++)"; scriptString=scriptString+"{"; scriptString=scriptString+" window.document.forms[0]."+DayName+".options(i-1).text=i;"; scriptString=scriptString+" window.document.forms[0]."+DayName+".options(i-1).value=i;"; scriptString=scriptString+" }"; scriptString=scriptString+"}"; scriptString=scriptString+"</script>"; this.Page.RegisterClientScriptBlock(id, scriptString); } DateTime now=DateTime.Today; ServerNowYear =now.Year ; if(!Page.IsPostBack) { ServerYear=ServerNowYear ; ServerMonth=now.Month; ServerDay=now.Day; ServerMonthDays=GetNowMonthDays(ServerYear,ServerMonth); } else { ServerYear=Convert.ToInt32(Request.Form[YearName]); ServerMonth=Convert.ToInt32(Request.Form[MonthName]); ServerDay= Convert.ToInt32(Request.Form[DayName]); ServerMonthDays=GetNowMonthDays(ServerYear,ServerMonth); } } private int GetNowMonthDays(int ServerYear,int ServerMonth) { int ServerMonthDays=31; if(ServerMonth==4||ServerMonth==6||ServerMonth==9||ServerMonth==11) ServerMonthDays=30; else if(ServerMonth==02) { if((ServerYear%4==0 &&ServerYear%100!=0) || ServerYear%400==0) ServerMonthDays=29; else ServerMonthDays=28; } return ServerMonthDays; } protected void FillOptions(int StartValue,int OptionsLength,int SelectedOption) { for(int j=StartValue;j<=OptionsLength;j++) { string ShowOption; if(j<10) ShowOption="0"+j.ToString(); else ShowOption=j.ToString(); if(j==SelectedOption) Response.Write(" <OPTION value="+ShowOption+" selected>"+ShowOption+"</OPTION>"); else Response.Write(" <OPTION value="+ShowOption+" >"+ShowOption+"</OPTION>"); } }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// 设计器支持所需的方法 - 不要使用 /// 代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
<%@ Register TagPrefix="uc1" TagName="YearMonthDayDownDropList" Src="Modules/YearMonthDayDownDropList.ascx" %> <%@ Page language="c#" Codebehind="WebForm7.aspx.cs" AutoEventWireup="false" Inherits="micrm.WebForm7" %>

|