Populate dropdown list

Hi,
I have 2 drop down lists that I would like to populate.

One dropdown list is Months and the other is Years.

I would like the Months dropdown to shown the last 12 months and the Year to show current year and previous year with both default selected on the current.

Any ideas?
DeveloperUserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TonyRebaCommented:
Try using ajax  is very easy to adapt in a asp.net page


http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Calendar/Calendar.aspx
0
crisco96Commented:
In your aspx page:
<asp:DropDownList runat="server" id="MonthDDL">
  <ListItem value="1" text="January" />
  <ListItem value="2" text="February" />
  <ListItem value="3" text="March" />
  <ListItem value="4" text="April" />
  <ListItem value="5" text="May" />
  <ListItem value="6" text="June" />
  <ListItem value="7" text="July" />
  <ListItem value="8" text="August" />
  <ListItem value="9" text="September" />
  <ListItem value="10" text="October" />
  <ListItem value="11" text="November" />
  <ListItem value="12" text="December" />
</asp:DropDownList>
<asp:DropDownList runat="server" id="YearDDL" />

Open in new window



In your code behind:
protected void Page_Load(object sender, EventArgs e)
{
     if(!Page.IsPostBack)
    {
         MonthDDL.SelectedValue = DateTime.Now.Month.ToString();

         for(int counter = 0; counter < 2; ++counter)
         {
              string yearToAdd = (DateTime.Now.Year - counter).ToString();
              YearDDL.Items.Add(new ListItem(yearToAdd))
         }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
santhimurthydCommented:
I have create belwo two drop down controls in the aspx page
 
<asp:DropDownList ID="ddlYear" runat="server" 
            onselectedindexchanged="ddlYear_SelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList>

            <asp:DropDownList ID="ddlMonth" runat="server"></asp:DropDownList>

Open in new window


Below the login in code behind to populate the months based on your requirment

 
string[] Months = new string[]{ "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = DateTime.Now.Year;
                PopulateMonth(year);
                ddlYear.Items.Add( new ListItem(year.ToString() , year.ToString()));
                if (DateTime.Now.Month < 12)
                {
                    year--;
                    ddlYear.Items.Add(new ListItem(year.ToString(), year.ToString()));
                }
            }
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateMonth(int.Parse(ddlYear.SelectedValue));
        }

        private void PopulateMonth(int year)
        {
            ddlMonth.Items.Clear();
            if (year == DateTime.Now.Year)
            {
                int MonthMaxCount = DateTime.Now.Month;
                for (int i = 1; i <= MonthMaxCount; i++)
                {
                    ddlMonth.Items.Add(new ListItem(Months[i - 1], i.ToString()));
                }
            }
            else
            {
                int MonthremaingCount = DateTime.Now.Month +1;
                for (; MonthremaingCount <= 12; MonthremaingCount++)
                {
                    ddlMonth.Items.Add(new ListItem(Months[MonthremaingCount - 1], MonthremaingCount.ToString()));
                }
            }
        }

Open in new window


Let us know, whether you able to meet your requirment
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.