ASP.NET dropdownlist with 12 months and spanning multiple years

Posted on 2008-11-03
Last Modified: 2008-11-03
I am using ASP.NET, C#, and IE.

I need to create a dropdownlist box filled with 12 months. I know how to fill it with a straight 12 months, starting with January, all for the same year.  But what I want to do is fill the dropdown box with 12 months, starting with the current month, and going forward 9 months and having 3 months in the rear.  So, since the first month will be November, the list would start with August 2008 and have the next 12 months.  But since after 5 months, it will be 2009, the dropdown box will have to have month with 2008 and 2009.  Thanks in advance for the help.
Question by:geoffsweb
    LVL 26

    Accepted Solution

    on the page load fill in the months in your dropdown using following logic
                      DateTime dt = DateTime.Now.AddMonths(-3);
                      for (int i = 1; i < 13; i++)
                            dt = dt.AddMonths(1);
    LVL 7

    Assisted Solution

    yo gotta load the dropdown dynamically.
    One way of doing this is to write a method similar to whats shown below and call it in the Page Load
     Sub LoadDates()
            Dim textToDisplay As String
            Dim startDate As Date = Now.AddMonths(-3)
            For i = 0 To 11
                textToDisplay = startDate.AddMonths(i).ToString("MMMM yyyy")
        End Sub

    Open in new window

    LVL 7

    Expert Comment

    Oops I was a little behind ragi. But as you can see, same concept.

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now