Responsive Ads Here

Monday, March 18, 2013

Day, Month and Year selection with drop-downs

In my recent project I have a requirement, calendar will be in drop down format. I mean user can select date, month and year using drop downs.
Basic conditions for that drop downs are 
  • Year Drop down consists last 100 years.
  • Leap year handling
  • Days will be visible according the corresponding month.
Aspx code:--


<asp:UpdatePanel runat="server" ID="PaymentDetailsDailyUpdatePanel">  
   <ContentTemplate>  
 <div class="payment-select-date">  
 <b>Select a date:</b><br />  
 <div class="select-date">  
   <span class="formstyle" style="z-index: 9;">  
     <asp:DropDownList ID="ddlYear" runat="server" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged"  
    AutoPostBack="true">  
     </asp:DropDownList>  
   </span>  
 </div>  
   <div class="select-date">  
     <span class="formstyle" style="z-index: 9;">  
     <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" DataTextField="MonthName"  
       DataValueField="MonthNumber" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">  
     </asp:DropDownList>  
   </span>  
 </div>  
        <div class="select-date">  
   <span class="formstyle" style="z-index: 9;">  
     <asp:DropDownList ID="ddlday" runat="server">  
     </asp:DropDownList>  
   </span>  
 </div>  
                 </div>  
 </ContentTemplate>  
   <Triggers>  
     <asp:AsyncPostBackTrigger ControlID="ddlMonth" EventName="SelectedIndexChanged" />  
     <asp:AsyncPostBackTrigger ControlID="ddlYear" EventName="SelectedIndexChanged" />  
      </Triggers>  
 </asp:UpdatePanel>  

Aspx.cs code:--


 if (!IsPostBack)  
   {  
          ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();          ddlYear.DataBind();  
          BindDateDrodowns();           
  }  
  protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue)));  
       ddlday.DataBind();  
     }  
     protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       BindDateDropdowns();  
     }  
     public void BindDateDropdowns()  
     {  
       //Populate DropDownLists  
       ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new  
       {  
         MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),  
         MonthNumber = a  
       });  
       ddlMonth.DataBind();  
       ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));  
       ddlday.DataBind();  
     }  

No comments:

Post a Comment