用dropdownlist做3个下拉框(年,月,日)
protectedvoidPage_Load(objectsender,EventArgse){for(inti=1;i<=31;i++){DropDownList4.I...
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)
{
DropDownList4.Items.RemoveAt(29);
DropDownList4.Items.RemoveAt(28);
}
我想实现3个下拉框(年,月,日),选中某个年和月后,日会相应的变化(31,30,29,28--1)
写了如上代码(备注:DropDownList2是年,DropDownList3是月,DropDownList4是日)
代码编译后可以通过,日初始为1-30.但是下拉年和月的时候,日不会变化...
求高手指出错误修改代码,或者有新的代码.能远程监控帮我解决的更好!
dropdownlist.autopostback=true 展开
{
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)
{
DropDownList4.Items.RemoveAt(29);
DropDownList4.Items.RemoveAt(28);
}
我想实现3个下拉框(年,月,日),选中某个年和月后,日会相应的变化(31,30,29,28--1)
写了如上代码(备注:DropDownList2是年,DropDownList3是月,DropDownList4是日)
代码编译后可以通过,日初始为1-30.但是下拉年和月的时候,日不会变化...
求高手指出错误修改代码,或者有新的代码.能远程监控帮我解决的更好!
dropdownlist.autopostback=true 展开
展开全部
把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 <= day; i++)
{
DropDownList4.Items.Add(new ListItem(i.ToString()));
}
}
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 <= day; i++)
{
DropDownList4.Items.Add(new ListItem(i.ToString()));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询