您的位置:首页 > 其它

俺改进的下拉列表日期选择控件

2005-01-31 09:40 567 查看
YearMonthDayDownDropList.ascx:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="YearMonthDayDownDropList.ascx.cs" Inherits="Wzg.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 Wzg
{
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 class YearMonthDayDownDropList : System.Web.UI.UserControl
{
//选择的年月日如:20021225
public string YearMonthDay
{
get
{
return Request.Form[YearName]+"-"+Request.Form[MonthName]+"-"+Request.Form[DayName];
}
set
{
InitializeYearMonthDay = value;
}

}
protected string InitializeYearMonthDay=""; //初始化日期
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)
{

if (InitializeYearMonthDay=="")
{
ServerYear=ServerNowYear ;
ServerMonth=now.Month;
ServerDay=now.Day;
ServerMonthDays=GetNowMonthDays(ServerYear,ServerMonth);
}
else
{
//string[] ymdarray = ymd.Split('-');
//ymdarray = ymd.Split('-');
DateTime ymd = Convert.ToDateTime(InitializeYearMonthDay);
ServerYear=ymd.Year;
ServerMonth=ymd.Month;
ServerDay=ymd.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 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐