C# 通过从其他下拉列表中选择来更新下拉列表项

C# 通过从其他下拉列表中选择来更新下拉列表项,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我想在更改其他下拉列表中的选定项时更新下拉列表的条目。我有三个代表天、月和年的下拉列表。我想要的是,当我更新月份时,我希望天下拉列表中的天相应地改变,例如,我在月DDL中选择4(即4月),我希望天DDL显示条目,直到30。这是我所做的,但没有效果 if (ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(9) || ddlmonth.Selec

我想在更改其他下拉列表中的选定项时更新下拉列表的条目。我有三个代表天、月和年的下拉列表。我想要的是,当我更新月份时,我希望天下拉列表中的天相应地改变,例如,我在月DDL中选择4(即4月),我希望天DDL显示条目,直到30。这是我所做的,但没有效果

if (ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(9) || ddlmonth.SelectedIndex.Equals(11))
{

    for(int i = 1; i <= 30; i++)
    {
        this.ddldate.Items.Add(i.ToString());
    }
}
if(ddlmount.SelectedIndex.Equals(6)| | ddlmount.SelectedIndex.Equals(6)| | ddlmount.SelectedIndex.Equals(9)| | ddlmount.SelectedIndex.Equals(11))
{

对于(int i=1;i您应该添加此函数void Index_Changed(对象发送方,事件参数e)
下面是一些测试代码

Aspx页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Temp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">Year
    <asp:DropDownList ID="_year" runat="server" onselectedindexchanged="_year_SelectedIndexChanged" AutoPostBack="true">
    </asp:DropDownList>Month
    <asp:DropDownList ID="_month" runat="server" onselectedindexchanged="_month_SelectedIndexChanged" AutoPostBack="true">
    </asp:DropDownList>Day
    <asp:DropDownList ID="_day" runat="server" onselectedindexchanged="_day_SelectedIndexChanged" AutoPostBack="true">
    </asp:DropDownList>
    </form>
</body>
</html>

年
月
白天
和aspx.cs文件代码是

namespace Temp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                _year.DataSource = GetData.getYear();
                _year.DataTextField = "Year";
                _year.DataBind();
                _month.Items.Add(new ListItem("Select"));
                _month.DataBind();
                _day.Items.Add(new ListItem("Select"));
                _day.DataBind();
            }
        }
        protected void _year_SelectedIndexChanged(object sender, EventArgs e)
        {
            _month.Items.Clear();
            _month.DataSource = GetData.getMonth();
            _month.DataTextField = "MonthName";
            _month.DataValueField = "MonthId";
            _month.DataBind();
        }
        protected void _month_SelectedIndexChanged(object sender, EventArgs e)
        {
            _day.Items.Clear();
            _day.DataSource = GetData.getDays(_month.SelectedIndex+1,GetData.isLeapYear(Convert.ToInt32(_year.SelectedValue)));
            _day.DataTextField = "Day";
            _day.DataBind();
        }
        protected void _day_SelectedIndexChanged(object sender, EventArgs e)
        {
           //Use selected value of _month _year and _day here
        }
    }    
    public class GetData
    {
        public static List<day> getDays(int monthId,bool isLeapYear)
        {
            int maxLimit;
            if (monthId == 2)
            {
                if (isLeapYear) maxLimit = 29;
                else maxLimit = 28;
            }
            else if (monthId == 1 || monthId == 3 || monthId == 5 || monthId == 7 || monthId == 8 || monthId == 10 || monthId == 12)
                maxLimit = 31;
            else maxLimit = 30;
            List<day> days = new List<day>();
            for (int i = 1; i <= maxLimit; i++)
            {
                days.Add(new day { Day = i });
            }
            return days;

        }
        public static List<year> getYear()
        {
            //Set here min and max range of year 
            int minLimit = 1950;
            int maxLimit = DateTime.Now.Year;
            List<year> years = new List<year>();
            for (int i = minLimit; i <= maxLimit; i++)
            {
                //You can modify code for bind year dropdown 
                //respectively change in date or month
                /*if (isLeapYear)
                {
                    if(i%4==0)
                        years.Add(new year { Year = i });
                }
                else*/
                years.Add(new year { Year = i });
            }
            return years;
        }
        public static List<month> getMonth()
        {
            List<month> months=new List<month>();            
            months.Add(new month { MonthId = 1, MonthName = "Jan" });
            months.Add(new month { MonthId = 2, MonthName = "Frb" }); months.Add(new month { MonthId = 3, MonthName = "Mar" });
            months.Add(new month { MonthId = 4, MonthName = "Apr" }); months.Add(new month { MonthId = 5, MonthName = "May" });
            months.Add(new month { MonthId = 6, MonthName = "Jun" }); months.Add(new month { MonthId = 7, MonthName = "July" }); 
            months.Add(new month { MonthId = 8, MonthName = "Aug" });
            months.Add(new month { MonthId = 9, MonthName = "Sep" }); months.Add(new month { MonthId = 10, MonthName = "Oct" });
            months.Add(new month { MonthId = 11, MonthName = "Nov" });months.Add(new month { MonthId = 12, MonthName = "Dec" });
            return months;
        }
        internal static bool isLeapYear(int p)
        {
            if (p % 4 == 0)
                return true;
            else return false;
        }
    }
    public class month
    {
        public string MonthName { get; set; }
        public int MonthId { get; set; }
    }
    public class day
    {
        public int Day { get; set; }
    }
    public class year
    {
        public int Year { get; set; }
    }
}
namespace-Temp
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
_year.DataSource=GetData.getYear();
_year.DataTextField=“year”;
_year.DataBind();
_添加(新列表项(“选择”));
_month.DataBind();
_添加(新列表项(“选择”));
_day.DataBind();
}
}
受保护的无效\u年\u选择的索引已更改(对象发件人,事件参数e)
{
_month.Items.Clear();
_month.DataSource=GetData.getMonth();
_month.DataTextField=“MonthName”;
_month.DataValueField=“MonthId”;
_month.DataBind();
}
受保护的void\u month\u SelectedIndexChanged(对象发送方,事件参数e)
{
_day.Items.Clear();
_day.DataSource=GetData.getDays(_month.SelectedIndex+1,GetData.isLeapYear(Convert.ToInt32(_year.SelectedValue));
_day.DataTextField=“day”;
_day.DataBind();
}
受保护的无效\u天\u选择的索引已更改(对象发送方,事件参数e)
{
//在此处使用所选的_月_年和_日值
}
}    
公共类GetData
{
公共静态列表getDays(int monthId,bool isLeapYear)
{
int-maxLimit;
if(monthId==2)
{
如果(isLeapYear)maxLimit=29;
else maxLimit=28;
}
如果(蒙希德==1 | |蒙希德==3 | |蒙希德==5 | |蒙希德==7 | |蒙希德==8 | |蒙希德==10 | |蒙希德==12)
maxLimit=31;
else maxLimit=30;
列表天数=新列表();

对于(int i=1;i)您是如何将下拉列表连接到事件处理程序的?请您澄清“这是我所做的但没有效果”的含义是什么?还请注意,您将if(ddlmonth.SelectedIndex.Equals(6))中的前两个表达式加倍我在每月DDL的SelectedIndexChanged事件中编写了第一段代码,因此当我更改每月DDL的索引时,日期DDL中的项目相应地更改,即每个月的30天或31天等,第二段代码编写在Page_Load事件中。感谢您指出双表达式。我有edited你的标题。请看“”,其中的共识是“不,他们不应该”。你特别提到了四月-你的代码中有一个拼写错误,你指定了六月(6)两次,而不是四月(4)。这可能是问题的一部分。我已经这样做了。代码是在本次活动中编写的,但无论如何感谢您的回答。@Hassan我建议您将这种日历与javascript或jQuery一起使用。
namespace Temp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                _year.DataSource = GetData.getYear();
                _year.DataTextField = "Year";
                _year.DataBind();
                _month.Items.Add(new ListItem("Select"));
                _month.DataBind();
                _day.Items.Add(new ListItem("Select"));
                _day.DataBind();
            }
        }
        protected void _year_SelectedIndexChanged(object sender, EventArgs e)
        {
            _month.Items.Clear();
            _month.DataSource = GetData.getMonth();
            _month.DataTextField = "MonthName";
            _month.DataValueField = "MonthId";
            _month.DataBind();
        }
        protected void _month_SelectedIndexChanged(object sender, EventArgs e)
        {
            _day.Items.Clear();
            _day.DataSource = GetData.getDays(_month.SelectedIndex+1,GetData.isLeapYear(Convert.ToInt32(_year.SelectedValue)));
            _day.DataTextField = "Day";
            _day.DataBind();
        }
        protected void _day_SelectedIndexChanged(object sender, EventArgs e)
        {
           //Use selected value of _month _year and _day here
        }
    }    
    public class GetData
    {
        public static List<day> getDays(int monthId,bool isLeapYear)
        {
            int maxLimit;
            if (monthId == 2)
            {
                if (isLeapYear) maxLimit = 29;
                else maxLimit = 28;
            }
            else if (monthId == 1 || monthId == 3 || monthId == 5 || monthId == 7 || monthId == 8 || monthId == 10 || monthId == 12)
                maxLimit = 31;
            else maxLimit = 30;
            List<day> days = new List<day>();
            for (int i = 1; i <= maxLimit; i++)
            {
                days.Add(new day { Day = i });
            }
            return days;

        }
        public static List<year> getYear()
        {
            //Set here min and max range of year 
            int minLimit = 1950;
            int maxLimit = DateTime.Now.Year;
            List<year> years = new List<year>();
            for (int i = minLimit; i <= maxLimit; i++)
            {
                //You can modify code for bind year dropdown 
                //respectively change in date or month
                /*if (isLeapYear)
                {
                    if(i%4==0)
                        years.Add(new year { Year = i });
                }
                else*/
                years.Add(new year { Year = i });
            }
            return years;
        }
        public static List<month> getMonth()
        {
            List<month> months=new List<month>();            
            months.Add(new month { MonthId = 1, MonthName = "Jan" });
            months.Add(new month { MonthId = 2, MonthName = "Frb" }); months.Add(new month { MonthId = 3, MonthName = "Mar" });
            months.Add(new month { MonthId = 4, MonthName = "Apr" }); months.Add(new month { MonthId = 5, MonthName = "May" });
            months.Add(new month { MonthId = 6, MonthName = "Jun" }); months.Add(new month { MonthId = 7, MonthName = "July" }); 
            months.Add(new month { MonthId = 8, MonthName = "Aug" });
            months.Add(new month { MonthId = 9, MonthName = "Sep" }); months.Add(new month { MonthId = 10, MonthName = "Oct" });
            months.Add(new month { MonthId = 11, MonthName = "Nov" });months.Add(new month { MonthId = 12, MonthName = "Dec" });
            return months;
        }
        internal static bool isLeapYear(int p)
        {
            if (p % 4 == 0)
                return true;
            else return false;
        }
    }
    public class month
    {
        public string MonthName { get; set; }
        public int MonthId { get; set; }
    }
    public class day
    {
        public int Day { get; set; }
    }
    public class year
    {
        public int Year { get; set; }
    }
}