首先看看效果:
年與月的數據源,較好定義,而日的數據源即需要根據年與月選擇之后,方可獲取到那年月的所有日數。如年:

List<
int> i_Year
{
get
{
List< int> y = new List< int>();
int Ny = DateTime.Now.Year;
for ( int i = 1953; i <= Ny; i++)
{
y.Add(i);
}
return y;
}
}
{
get
{
List< int> y = new List< int>();
int Ny = DateTime.Now.Year;
for ( int i = 1953; i <= Ny; i++)
{
y.Add(i);
}
return y;
}
}
月的數據源:

List<
int> i_Month
{
get
{
List< int> m = new List< int>();
for ( int i = 1; i <= 12; i++)
{
m.Add(i);
}
return m;
}
}
{
get
{
List< int> m = new List< int>();
for ( int i = 1; i <= 12; i++)
{
m.Add(i);
}
return m;
}
}
日的數據源:

public List<
int> i_Day(
int year,
int month)
{
int days = DateTime.DaysInMonth(year, month);
List< int> d = new List< int>();
for ( int i = 1; i <= days; i++)
{
d.Add(i);
}
return d;
}
{
int days = DateTime.DaysInMonth(year, month);
List< int> d = new List< int>();
for ( int i = 1; i <= days; i++)
{
d.Add(i);
}
return d;
}
由於多個地方是DropDownList數據綁定,因此Insus.NET先寫一個通用DropDownList數據綁定的方法:

private
void DropDownlistParse(DropDownList ddl,
object dataSource,
string dataTextField,
string dataValueField)
{
ddl.DataSource = dataSource;
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataBind();
ddl.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
{
ddl.DataSource = dataSource;
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataBind();
ddl.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
這樣的話,在需要綁定的地方,即管傳入參數即可。如在Page_Load時,需要對年與月的DropDownList數據綁定:

protected
void Page_Load(
object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
DropDownlistParse( this.DropDownListYear, i_Year.Select(y => new { value = y }).ToList(), " value ", " value ");
DropDownlistParse( this.DropDownListMonth, i_Month.Select(m => new { value = m }).ToList(), " value ", " value ");
this.DropDownListDay.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
DropDownlistParse( this.DropDownListYear, i_Year.Select(y => new { value = y }).ToList(), " value ", " value ");
DropDownlistParse( this.DropDownListMonth, i_Month.Select(m => new { value = m }).ToList(), " value ", " value ");
this.DropDownListDay.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
在年或月DropDownlist控件有異動時,作相應對日的DropDownList控件數據綁定:

protected
void DropDownListYear_SelectedIndexChanged(
object sender, EventArgs e)
{
DayDataBinding();
}
protected void DropDownListMonth_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
private void DayDataBinding()
{
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty ( this.DropDownListYear.SelectedItem.Value)) return;
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value)) return;
int y = Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
int m = Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
DropDownlistParse( this.DropDownListDay, i_Day(y, m).Select(d => new { value = d }).ToList(), " value ", " value ");
}
{
DayDataBinding();
}
protected void DropDownListMonth_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
private void DayDataBinding()
{
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty ( this.DropDownListYear.SelectedItem.Value)) return;
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value)) return;
int y = Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
int m = Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
DropDownlistParse( this.DropDownListDay, i_Day(y, m).Select(d => new { value = d }).ToList(), " value ", " value ");
}
我們還要為了獲取年月日三個DropDownList控件的值,因此寫還得寫三個屬性:

private
int? _year;
private int? _month;
private int? _day;
public int? Year
{
get
{
if (ViewState[ " Year "] != null)
{
return Convert.ToInt32(ViewState[ " Year "]);
}
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListYear.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
}
set
{
ViewState[ " Year "] = value;
}
}
public int? Month
{
get
{
if (ViewState[ " Month "] != null)
{
return Convert.ToInt32(ViewState[ " Month "]);
}
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
}
set
{
ViewState[ " Month "] = value;
}
}
public int? Day
{
get
{
if (ViewState[ " Day "] != null)
{
return Convert.ToInt32(ViewState[ " Day "]);
}
if ( this.DropDownListDay.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListDay.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListDay.SelectedItem.Value);
}
set
{
ViewState[ " Day "] = value;
}
}
private int? _month;
private int? _day;
public int? Year
{
get
{
if (ViewState[ " Year "] != null)
{
return Convert.ToInt32(ViewState[ " Year "]);
}
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListYear.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
}
set
{
ViewState[ " Year "] = value;
}
}
public int? Month
{
get
{
if (ViewState[ " Month "] != null)
{
return Convert.ToInt32(ViewState[ " Month "]);
}
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
}
set
{
ViewState[ " Month "] = value;
}
}
public int? Day
{
get
{
if (ViewState[ " Day "] != null)
{
return Convert.ToInt32(ViewState[ " Day "]);
}
if ( this.DropDownListDay.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListDay.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListDay.SelectedItem.Value);
}
set
{
ViewState[ " Day "] = value;
}
}
最后是ASCX的html:

<%
@ Control Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
InsusDate.ascx.cs
"
Inherits
=
"
InsusDate
"
%>
< asp:DropDownList ID ="DropDownListYear" runat ="server" AutoPostBack ="true" OnSelectedIndexChanged ="DropDownListYear_SelectedIndexChanged" Width ="60px" ></ asp:DropDownList >年 < asp:DropDownList ID ="DropDownListMonth" runat ="server" AutoPostBack ="true" OnSelectedIndexChanged ="DropDownListMonth_SelectedIndexChanged" Width ="40PX" ></ asp:DropDownList >月 < asp:DropDownList ID ="DropDownListDay" runat ="server" Width ="40PX" ></ asp:DropDownList >日
< asp:DropDownList ID ="DropDownListYear" runat ="server" AutoPostBack ="true" OnSelectedIndexChanged ="DropDownListYear_SelectedIndexChanged" Width ="60px" ></ asp:DropDownList >年 < asp:DropDownList ID ="DropDownListMonth" runat ="server" AutoPostBack ="true" OnSelectedIndexChanged ="DropDownListMonth_SelectedIndexChanged" Width ="40PX" ></ asp:DropDownList >月 < asp:DropDownList ID ="DropDownListDay" runat ="server" Width ="40PX" ></ asp:DropDownList >日
完整的ASCX.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class InsusDate : System.Web.UI.UserControl
{
private int? _year;
private int? _month;
private int? _day;
public int? Year
{
get
{
if (ViewState[ " Year "] != null)
{
return Convert.ToInt32(ViewState[ " Year "]);
}
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListYear.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
}
set
{
ViewState[ " Year "] = value;
}
}
public int? Month
{
get
{
if (ViewState[ " Month "] != null)
{
return Convert.ToInt32(ViewState[ " Month "]);
}
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
}
set
{
ViewState[ " Month "] = value;
}
}
public int? Day
{
get
{
if (ViewState[ " Day "] != null)
{
return Convert.ToInt32(ViewState[ " Day "]);
}
if ( this.DropDownListDay.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListDay.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListDay.SelectedItem.Value);
}
set
{
ViewState[ " Day "] = value;
}
}
protected void Page_Load( object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
DropDownlistParse( this.DropDownListYear, i_Year.Select(y => new { value = y }).ToList(), " value ", " value ");
DropDownlistParse( this.DropDownListMonth, i_Month.Select(m => new { value = m }).ToList(), " value ", " value ");
this.DropDownListDay.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
protected void DropDownListYear_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
protected void DropDownListMonth_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
private void DayDataBinding()
{
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty ( this.DropDownListYear.SelectedItem.Value)) return;
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value)) return;
int y = Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
int m = Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
DropDownlistParse( this.DropDownListDay, i_Day(y, m).Select(d => new { value = d }).ToList(), " value ", " value ");
}
private void DropDownlistParse(DropDownList ddl, object dataSource, string dataTextField, string dataValueField)
{
ddl.DataSource = dataSource;
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataBind();
ddl.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
List< int> i_Year
{
get
{
List< int> y = new List< int>();
int Ny = DateTime.Now.Year;
for ( int i = 1953; i <= Ny; i++)
{
y.Add(i);
}
return y;
}
}
List< int> i_Month
{
get
{
List< int> m = new List< int>();
for ( int i = 1; i <= 12; i++)
{
m.Add(i);
}
return m;
}
}
public List< int> i_Day( int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
List< int> d = new List< int>();
for ( int i = 1; i <= days; i++)
{
d.Add(i);
}
return d;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class InsusDate : System.Web.UI.UserControl
{
private int? _year;
private int? _month;
private int? _day;
public int? Year
{
get
{
if (ViewState[ " Year "] != null)
{
return Convert.ToInt32(ViewState[ " Year "]);
}
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListYear.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
}
set
{
ViewState[ " Year "] = value;
}
}
public int? Month
{
get
{
if (ViewState[ " Month "] != null)
{
return Convert.ToInt32(ViewState[ " Month "]);
}
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
}
set
{
ViewState[ " Month "] = value;
}
}
public int? Day
{
get
{
if (ViewState[ " Day "] != null)
{
return Convert.ToInt32(ViewState[ " Day "]);
}
if ( this.DropDownListDay.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListDay.SelectedItem.Value))
return null;
else
return Convert.ToInt32( this.DropDownListDay.SelectedItem.Value);
}
set
{
ViewState[ " Day "] = value;
}
}
protected void Page_Load( object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
DropDownlistParse( this.DropDownListYear, i_Year.Select(y => new { value = y }).ToList(), " value ", " value ");
DropDownlistParse( this.DropDownListMonth, i_Month.Select(m => new { value = m }).ToList(), " value ", " value ");
this.DropDownListDay.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
protected void DropDownListYear_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
protected void DropDownListMonth_SelectedIndexChanged( object sender, EventArgs e)
{
DayDataBinding();
}
private void DayDataBinding()
{
if ( this.DropDownListYear.SelectedIndex == - 1 || string.IsNullOrEmpty ( this.DropDownListYear.SelectedItem.Value)) return;
if ( this.DropDownListMonth.SelectedIndex == - 1 || string.IsNullOrEmpty( this.DropDownListMonth.SelectedItem.Value)) return;
int y = Convert.ToInt32( this.DropDownListYear.SelectedItem.Value);
int m = Convert.ToInt32( this.DropDownListMonth.SelectedItem.Value);
DropDownlistParse( this.DropDownListDay, i_Day(y, m).Select(d => new { value = d }).ToList(), " value ", " value ");
}
private void DropDownlistParse(DropDownList ddl, object dataSource, string dataTextField, string dataValueField)
{
ddl.DataSource = dataSource;
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataBind();
ddl.Items.Insert( 0, new ListItem( string.Empty, string.Empty));
}
List< int> i_Year
{
get
{
List< int> y = new List< int>();
int Ny = DateTime.Now.Year;
for ( int i = 1953; i <= Ny; i++)
{
y.Add(i);
}
return y;
}
}
List< int> i_Month
{
get
{
List< int> m = new List< int>();
for ( int i = 1; i <= 12; i++)
{
m.Add(i);
}
return m;
}
}
public List< int> i_Day( int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
List< int> d = new List< int>();
for ( int i = 1; i <= days; i++)
{
d.Add(i);
}
return d;
}
}
可以把ASCX保存為一個ASCX用戶控件,在網頁拉進去即可。