zoukankan      html  css  js  c++  java
  • DropDownList自动生成年月日

     DropDownList自动生成年月日

     

     

     

    aspx页面上有三个dropdownlist控件, 

    dropdownlist1 表示年,dropdownlist2表示月,dropdownlist3表示天; 
    注意用将这三个dropdownlist控件的autopostback属性设为true。 

    用户能够方便地选择年月日,并且每月的日期会随着用户选择不同的年,月而发生相应的变化 

    代码直接复制过去即可使用


    前台代码

    <div>

            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 

                onselectedindexchanged="DropDownList1_SelectedIndexChanged">

            </asp:DropDownList>

            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

                onselectedindexchanged="DropDownList2_SelectedIndexChanged1">

            </asp:DropDownList>

            <asp:DropDownList ID="DropDownList3" runat="server">

            </asp:DropDownList>

    </div>

     

    后台代码 

    protected void Page_Load(object sender, EventArgs e)

            {

                DateTime tnow = DateTime.Now;//现在时间 

                ArrayList alyear = new ArrayList();

                int i;

                for (i = 2002; i <= 2010; i++)

                    alyear.Add(i);

                ArrayList almonth = new ArrayList();

                for (i = 1; i <= 12; i++)

                    almonth.Add(i);

                if (!this.IsPostBack)

                {

     

                    DropDownList1.DataSource = alyear;

                    DropDownList1.DataBind();//绑定年 

                    //选择当前年 

                    DropDownList1.SelectedValue = tnow.Year.ToString();

                    DropDownList2.DataSource = almonth;

                    DropDownList2.DataBind();//绑定月 

                    //选择当前月 

                    DropDownList2.SelectedValue = tnow.Month.ToString();

                    int year, month;

                    year = Int32.Parse(DropDownList1.SelectedValue);

                    month = Int32.Parse(DropDownList2.SelectedValue);

                    binddays(year, month);//绑定天 

                    //选择当前日期 

                    DropDownList3.SelectedValue = tnow.Day.ToString();

                }

            }

     

     

            //判断闰年 

            private bool checkleap(int year)

            {

                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))

                    return true;

                else return false;

            }

            //绑定每月的天数 

            private void binddays(int year, int month)

            {

                int i;

                ArrayList alday = new ArrayList();

     

                switch (month)

                {

                    case 1:

                    case 3:

                    case 5:

                    case 7:

                    case 8:

                    case 10:

                    case 12:

                        for (i = 1; i <= 31; i++)

                            alday.Add(i);

                        break;

                    case 2:

                        if (checkleap(year))

                        {

                            for (i = 1; i <= 29; i++)

                                alday.Add(i);

                        }

                        else

                        {

                            for (i = 1; i <= 28; i++)

                                alday.Add(i);

                        }

                        break;

                    case 4:

                    case 6:

                    case 9:

                    case 11:

                        for (i = 1; i <= 30; i++)

                            alday.Add(i);

                        break;

                }

                DropDownList3.DataSource= alday;

                DropDownList3.DataBind();

            }

     

            //选择年 

            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

            {

                int year, month;

                year = Int32.Parse(DropDownList1.SelectedValue);

                month = Int32.Parse(DropDownList2.SelectedValue);

                binddays(year, month);

            }

     

            //选择月 

            protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)

            {

                int year, month;

                year = Int32.Parse(DropDownList1.SelectedValue);

                month = Int32.Parse(DropDownList2.SelectedValue);

                binddays(year, month);

            }

     comboBox控件自动生成年月日 

            //判断闰年

            private bool checkleap(int year)

            {

                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))

                    return true;

                else return false;

            }

            //绑定每月的天数

            private void binddays(int year, int month)

            {

                int i;

                ArrayList alday = new ArrayList();

                 switch (month)

                {

                    case 1:

                    case 3:

                    case 5:

                    case 7:

                    case 8:

                    case 10:

                    case 12:

                        for (i = 1; i <= 31; i++)

                            alday.Add(i);

                        break;

                    case 2:

                        if (checkleap(year))

                        {

                            for (i = 1; i <= 29; i++)

                                alday.Add(i);

                        }

                        else

                        {

                            for (i = 1; i <= 28; i++)

                                alday.Add(i);

                        }

                        break;

                    case 4:

                    case 6:

                    case 9:

                    case 11:

                        for (i = 1; i <= 30; i++)

                            alday.Add(i);

                        break;

                }

                this.comboBox3.DataSource = alday;

     

            }

            private void Form1_Load(object sender, EventArgs e)

            {          

                //绑定年

                ArrayList alyear = new ArrayList();

                int i;

                for (i = 1900; i <= 2099; i++)

                    alyear.Add(i);

                this.comboBox1.DataSource = alyear;          

                //绑定月

                ArrayList almonth = new ArrayList();

                for (i = 1; i <= 12; i++)

                    almonth.Add(i);

                this.comboBox2.DataSource = almonth;

     

                int year, month;

                year = Int32.Parse(this.comboBox1.SelectedValue.ToString());

                month = Int32.Parse(this.comboBox2.SelectedValue.ToString());

                binddays(year, month);//绑定天

             }

     

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

            {

                int year, month;

                year = Int32.Parse(this.comboBox1.SelectedValue.ToString());

                month = 1;

                binddays(year, month);

            }

     

            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

            {

                int year, month;

                year = Int32.Parse(this.comboBox1.SelectedValue.ToString());

                month = Int32.Parse(this.comboBox2.SelectedValue.ToString());

                binddays(year, month);

            }

     

  • 相关阅读:
    JavaScript 本地对象、内置对象、宿主对象
    数据交换格式
    网页设计之内容、结构、表现分离
    Web前端浏览器兼容初探
    javascript call()与apply()
    天气API
    display:inline,display:inline-block,display:block 区别
    javascript sort()与reverse()
    The Primo ScholarRank Technology: Bringing the Most Relevant Results to the Top of the List
    IOS ——OC—— NSMutableDictionary的使用总结
  • 原文地址:https://www.cnblogs.com/quanxie/p/3283133.html
Copyright © 2011-2022 走看看