年月日 DropDownList

来源:互联网 发布:淘宝上的手工皮鞋店铺 编辑:程序博客网 时间:2024/06/05 06:20

回答问题顺手写了一个:

<!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>    <style type="text/css">        input, textarea {width: 100px;border-top: solid 1px #999;border-right: solid 1px #CCC;border-left: solid 1px #CCC;                    border-bottom: solid 1px #EEE;padding: 5px 8px;border-radius: 2px;line-height: 12px;color: #999;vertical-align: middle;}        select {border-top: solid 1px #999;border-right: solid 1px #CCC;border-left: solid 1px #CCC;border-bottom: solid 1px #EEE;                padding: 5px 8px;border-radius: 2px;color: #999;vertical-align: middle;}        .button{width: 67px; height: 30px; border: 0 none; background-color: #FFAE00; font-weight: bold; color: white;}    </style>    </head><body>    <form id="form1" runat="server">    <div class="content">        <p><asp:TextBox runat="server" ID="txty" style="width:60px;" Text="2008" />         <asp:TextBox runat="server" ID="txtm" style="width:60px;" Text="8" />         <asp:TextBox runat="server" ID="txtd" style="width:60px;" Text="8" />         <asp:Button runat="server" ID="btnTest" CssClass="button" Text="测试" onclick="btnTest_Click" /></p>        <p>                        <asp:DropDownList runat="server" ID="ddl_y" Width="118" AutoPostBack="true"                 onselectedindexchanged="ddl_y_SelectedIndexChanged"></asp:DropDownList>            <asp:DropDownList runat="server" ID="ddl_m" Width="118" AutoPostBack="true"                 onselectedindexchanged="ddl_m_SelectedIndexChanged"></asp:DropDownList>            <asp:DropDownList runat="server" ID="ddl_d" Width="118"></asp:DropDownList>        </p>    </div>    </form>    </body></html>

protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                ddlBindY();                ddlBindM();                BindDays(Int32.Parse(ddl_y.SelectedValue), Int32.Parse(ddl_m.SelectedValue));            }        }        public DateTime tnow = DateTime.Now;//现在时间        public void ddlBindY()        {            ArrayList AlYear = new ArrayList();            for (int i = tnow.Year; i >= 2000; i--) AlYear.Add(i);            //绑定年            ddl_y.DataSource = AlYear;            ddl_y.DataBind();            ddl_y.SelectedValue = tnow.Year.ToString();        }        public void ddlBindM()        {            ArrayList AlMonth = new ArrayList();            for (int i = 1; i <= 12; i++) AlMonth.Add(i);            //绑定月            ddl_m.DataSource = AlMonth;            ddl_m.DataBind();            ddl_m.SelectedValue = tnow.Month.ToString();        }        private void BindDays(int year, int month)        {            int days = DateTime.DaysInMonth(year, month);            ArrayList ardate = new ArrayList();            for (int i = 1; i <= days; i++) ardate.Add(i);            ddl_d.DataSource = ardate;            ddl_d.DataBind();    ddl_d.SelectedValue = tnow.Day.ToString();        }        protected void ddl_y_SelectedIndexChanged(object sender, EventArgs e)        {            BindDays(Int32.Parse(ddl_y.SelectedValue), Int32.Parse(ddl_m.SelectedValue));        }        protected void ddl_m_SelectedIndexChanged(object sender, EventArgs e)        {            BindDays(Int32.Parse(ddl_y.SelectedValue), Int32.Parse(ddl_m.SelectedValue));        }        //绑定        protected void btnTest_Click(object sender, EventArgs e)        {            ddl_y.SelectedValue = this.txty.Text;            ddl_m.SelectedValue = this.txtm.Text;            BindDays(Int32.Parse(ddl_y.SelectedValue), Int32.Parse(ddl_m.SelectedValue));            ddl_d.SelectedValue = this.txtm.Text;        }


原创粉丝点击