0

当我更改另一个下拉列表中的选定项目时,我想更新下拉列表的条目。我有三个下拉列表,分别代表天、月和年天 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());
    }
}

我已经为所有 3 个 DDL 启用了自动回发,但仍然没有效果。并且在 Page_Load 上我添加了接下来的年份和月份可供选择。

if (!IsPostBack)
{
    for (int i = 1990; i < 2021; i++)
        this.ddlyear.Items.Add(i.ToString());

    for (int i = 1; i <= 12; i++)
        this.ddlmonth.Items.Add(i.ToString());

}
4

2 回答 2

0

你应该添加这个函数 void Index_Changed(Object sender, EventArgs e) http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.onselectedindexchanged.aspx

于 2013-10-08T21:00:38.683 回答
0

这是一些测试代码的东西

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; }
    }
}
于 2013-10-10T22:30:40.790 回答