ASP.NET dropdownlist with 12 months and spanning multiple years

Posted on 2008-11-03
Medium Priority
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
  • 2
LVL 26

Accepted Solution

Anurag Thakur earned 252 total points
ID: 22868826
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);

Assisted Solution

the_bachelor earned 248 total points
ID: 22868848
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


Expert Comment

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

Author Comment

ID: 22869894

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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