用dropdownlist做3个下拉框(年,月,日)

来源:百度知道 编辑:UC知道 时间:2024/07/14 02:03:12
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 31; i++)
{
DropDownList4.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

}
}

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string[] month1 = new string[] { "4", "6", "9", "11" };
string month2 = "2";

foreach (string s in month1)
{
if (DropDownList3.SelectedItem.Value == s)
DropDownList4.Items.RemoveAt(30);
break;
}

if (DropDownList3.Text == month2 && Convert.ToInt32(DropDownList2.Text) % 4 != 0)
DropDownList4.Items.RemoveAt(29);
if (DropDownList3.Text == month2 && Convert.ToInt32(DropDownList2.Text) % 4 == 0)
{
DropDownLi

把DropDownList3_SelectedIndexChanged替换成下面的试试,你自己可以优化或者修改一下,我增加了一个函数,供调用,自认为比你的想法要好一点,呵呵

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string[] month1 = new string[] { "4", "6", "9", "11" };
string month2 = "2";

DownList4(31);
foreach (string s in month1)
{
if (DropDownList3.SelectedItem.Value == s)
DownList4(30);
break;
}
if (DropDownList3.Text == month2 && Convert.ToInt32(DropDownList2.Text) % 4 != 0)
DownList4(29);
if (DropDownList3.Text == month2 && Convert.ToInt32(DropDownList2.Text) % 4 == 0)
{
DownList4(28);
}

}
protected void DownList4(int day)
{
DropDownList4.Items.Clear();
for (int i = 1; i