用多个DropDownList分别来绑定多个年月日
2012-10-16 00:08
246 查看
首先看看效果:
View Code
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;
}
}
可以把ASCX保存为一个ASCX用户控件,在网页拉进去即可。
View Code
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;
}
}
可以把ASCX保存为一个ASCX用户控件,在网页拉进去即可。
相关文章推荐
- dropdownlist绑定的二种方法
- DropdownList绑定的两种方法
- 遍历文件夹里面的文件,并绑定到dropdownlist
- 解决获取DropDownList绑定的记录值,获得的总是第一个记录?
- DropDownList绑定枚举类型
- C#遍历枚举,并绑定至类似DropDownList的控件上。
- DropdownList绑定的两种方法
- 牛腩购物15:中国省市县数据库,DropDownList 绑定数据库 下拉选择省市,购物车的实现,使用sqlserver 2008 导入access数据库,不能在 DropDownList 中选择多个项
- Demo: Dictionary>> 泛型读取XML文件数据作为数据源(DataSource)绑定到DropDownList控件 定义个一个公用类
- 让DropDownList绑定系统颜色
- FormView控件下DropDownList是否可以绑定
- 请分别输入年月日三个数值,计算出这是这一年的第几天? (提示:要考虑闰年和2月的情况)
- DataBinding?资料绑定? #3--以DropDownList为例
- DropDownList,在Init 事件中编写代码,绑定数据表
- gridview行编辑时绑定Dropdownlist并选中对应值
- 也谈谈动态绑定dropdownlist(2)
- 数据绑定控件收藏 DropDownList ListBox GridView DataList Repeater FormView DetailsView
- DropDownList、ListControl、RadioButtonList绑定
- DropDownList 绑定
- DropDownList绑定Xml数据的实现