C#日期用户控件代码

本文介绍了一个自定义的日期选择器组件实现方法,该组件能够动态更新年、月、日选项,并考虑了不同月份及闰年的天数变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发的时候遇到的时间日期问题,后来在网上找到了这个很好用的,所以共享出来,希望对大家有帮助!

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
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值