webform的三级联动
与winform一样,只不过需把DropDownList的AutoPostBack属性改为True。
*简单日期的编写方法:
用是三个DropDownList分别代表年月日,用for循环进行数据绑定,同时并对日进行判断
for (int i = DateTime.Now.Year; i >= 1900; i--) { ListItem li = new ListItem(i.ToString()); Year_Dr.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString()); Mouth_dr.Items.Add(li); } Days(); Mouth_dr.SelectedValue = DateTime.Now.Month.ToString(); Day_dr.SelectedValue = DateTime.Now.Day.ToString(); } Year_Dr.SelectedIndexChanged +=Year_Dr_SelectedIndexChanged; Mouth_dr.SelectedIndexChanged +=Mouth_dr_SelectedIndexChanged; //方法及事件 private void Days() { for (int i = 1; i <= 31; i++) { int year = Convert.ToInt32(Year_Dr.SelectedValue); int mouth = Convert.ToInt32(Mouth_dr.SelectedValue); if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) { } else if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) { if (i == 31) { break; } } else if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { if (i == 30) { break; } } else { if (i == 29) { break; } } ListItem li = new ListItem(i.ToString()); Day_dr.Items.Add(li); } } void Mouth_dr_SelectedIndexChanged(object sender, EventArgs e) { Day_dr.Items.Clear(); Days(); } void Year_Dr_SelectedIndexChanged(object sender, EventArgs e) { Mouth_dr.Items.Clear(); Day_dr.Items.Clear(); for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString()); Mouth_dr.Items.Add(li); } Days(); }