俺改进的下拉列表日期选择控件
来源:互联网 发布:js null 0 undefined 编辑:程序博客网 时间:2024/05/16 05:42
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
}
}
- 俺改进的下拉列表日期选择控件
- Eric的日期选择下拉列表小控件
- Eric的日期选择下拉列表小控件
- Eric的日期选择下拉列表小控件
- 日期选择下拉列表小控件
- jQuery插件 - 下拉列表日期选择控件
- 打造完美SWT日期选择控件:(四)下拉列表的整体设计
- 下拉列表选择控件, 用于替换原生的 select 控件
- 两个不错的下拉式日期选择控件
- 两个不错的下拉式日期选择控件
- 打造完美SWT日期选择控件:(三)看上去像下拉列表
- 改进了的日期控件
- 创建下拉式日期选择控件
- JAVA Swing日期下拉选择控件(面板)
- MY97日期选择控件支持日期CTRL多选(改进)
- 下拉列表,日期选择器,时间选择器,单项选择,多项选择
- bootstrap datetimepicker 选择实现 下拉列表选日期
- 下拉的选择日期控件(user control ),功能完善,修改了bug
- 黄金十二宫之金牛宫
- CString到UNICODE的转换
- 黄金十二宫之双子宫
- 用Delphi创建服务程序
- win2000server IIS和tomcat5多站点配置
- 俺改进的下拉列表日期选择控件
- Windows下JSP开发环境的配置
- A Key SQL Server 2005 developer's blog
- 第 7 页 初始化与赋值
- Java 性能优化技巧集锦 (1)
- Java 性能优化技巧集锦 (2)
- 英格兰七国志
- VB使用堆叠对话框的一个方法
- Java 性能优化技巧集锦 (3)