How to fill dropdownlist with days, month and year in asp.net(C#, VB)

Introduction: In previous articles i explained How to fill DropDownList from Sql server database in asp.net and how to Fill CheckBoxList based on DropDownList selection in asp.net(C#, VB) and How to open Pop up window on Drop down selection in Asp.net. Now in this article I am going to explain one of the common requirements while creating user input form like registration form where user has to select his date of birth, joining date etc.

Description: So here in this example I have demonstrated with example How to fill/ load/ bind years, months and days in the DropDownList . Current date will be selected in the DropDownList by default and when user select another month from the DropDownList then corresponding number of days will be filled in the days DropDownList.

fill day month year in dropdownlist example in asp.net

Implementation: Let's create an asp.net application to understand.
  • In the design page(.aspx) place 3 DropDownList for year, month and days as:
 <fieldset style="width:300px">
    <legend>Select Date</legend>
    Year: <asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True"
            onselectedindexchanged="ddlYear_SelectedIndexChanged" ></asp:DropDownList> 
           
    Month: <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="True"
            onselectedindexchanged="ddlMonth_SelectedIndexChanged">
        </asp:DropDownList> 
       
    Day: <asp:DropDownList ID="ddlDay" runat="server">
        </asp:DropDownList>
    </fieldset>

  • In the Code behind file(.aspx.cs) write the code as:
C#.NET Code to fill dropdownlist with days, months and years in asp.net

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Fill Years
            for (int i = 2013; i <= 2020; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
            ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true//set current year as selected

            //Fill Months
            for (int i = 1; i <= 12; i++)
            {
                ddlMonth.Items.Add(i.ToString());
            }
            ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected = true; // Set current month as selected

            //Fill days
            FillDays();
        }
    }
    public void FillDays()
    {
        ddlDay.Items.Clear();
        //getting numbner of days in selected month & year
        int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));

        //Fill days
        for (int i = 1; i <= noofdays; i++)
        {
            ddlDay.Items.Add(i.ToString());
        }
        ddlDay.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;// Set current date as selected
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDays();
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
 FillDays();
}
  • In the Code behind file(.aspx.vb) write the code as:
VB.NET Code to fill dropdownlist with days, months and years in asp.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Fill Years
            For i As Integer = 2013 To 2020

                ddlYear.Items.Add(i.ToString())
            Next
            ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = True  'set current year as selected

            'Fill Months
            For i As Integer = 1 To 12
                ddlMonth.Items.Add(i.ToString())
            Next
            ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected = True  ' Set current month as selected
            'Fill days
            FillDays()
        End If
    End Sub
    Public Sub FillDays()
        ddlDay.Items.Clear()
        'getting numbner of days in selected month & year
        Dim noofdays As Integer = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue))

        'Fill days
        For i As Integer = 1 To noofdays
            ddlDay.Items.Add(i.ToString())
        Next

        ddlDay.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = True ' Set current date as selected
    End Sub
    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        FillDays()
    End Sub
    Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        FillDays()
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

18 comments

Click here for comments
Anonymous
admin
September 30, 2013 ×

Do you have a code for Month and Year Drop downs only... and what is the best way to save this data in sql server, should we use datetime to format it and save in a single field or add three different fields in database to store.. what is the best approach and what would you suggest. Thanks

Reply
avatar
Unknown
admin
October 12, 2013 ×

Thank you friend it was very much useful for me but simple change i want that is it has to display default as month,day,year not current then how to do.

Reply
avatar
October 13, 2013 ×

Hi Krishna Murthy..thanks for appreciating my work..i suggest you to read the article : http://www.webcodeexpert.com/2013/07/bindsaveeditupdatecanceldeletepaging.html

this article has the current date filled..

Reply
avatar
Being Happy
admin
April 03, 2014 ×

Thanks very much

Reply
avatar
April 03, 2014 ×

Your welcome Phearin Mey..stay connected and keep reading..:)

Reply
avatar
Anonymous
admin
April 22, 2014 ×

Thanks

Reply
avatar
burning.bits
admin
June 06, 2014 ×

Lalit Raghuvanshi excellent article, thanks for sharing. greetings from Monterrey Mexico

Reply
avatar
June 07, 2014 ×

thanks for your feedback..i am glad you found this article useful...

Reply
avatar
Anonymous
admin
July 19, 2014 ×

Beautiful work, thank you.

Danny

Reply
avatar
July 19, 2014 ×

Thanks Danny..keep reading..:)

Reply
avatar
Unknown
admin
August 28, 2015 ×

Awesome...thats what I need....Thanks a lot :D

Reply
avatar
October 20, 2015 ×

thank you so much for this code

Reply
avatar
October 24, 2015 ×

Thanks for your valuable feedback..stay connected and keep reading

Reply
avatar
December 12, 2015 ×

Thanks ambadas for your valuable comment. Stay connected and keep reading for more useful updates..:)

Reply
avatar
nitinkumar
admin
November 22, 2019 ×

Its very helpful!

Thanks

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..