Solved

Populating dropdownlist in pageload dupliactes items

Posted on 2008-06-18
6
464 Views
Last Modified: 2012-05-05
Hi, I'm want to populate a dropdownlist with this year and the next 5 years.
So in the pageload I have added the code as shown in the snippet.

This I had hoped would give me the list 2008,2009,2010,2011,2012,2013 but it actually only gives me a list of 2013 six times.

Can't quite work it out any help appreciated.


DateTime dt = DateTime.Now;

        int year = dt.Year;
 

        ListItem addYear = new ListItem();

        for (int i = 0; i <= 5; i++)

        {

            addYear.Value = Convert.ToString(year + i);

            addYear.Text = Convert.ToString(year + i);

            StartYear.Items.Add(addYear);

            

        }

Open in new window

0
Comment
Question by:fusioninternet
  • 4
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21811653

 DateTime dt = DateTime.Now;
        int year = dt.Year;
 
       
        for (int i = 0; i <= 5; i++)
        {
            ListItem addYear = new ListItem();
            addYear.Value = Convert.ToString(year + i);
            addYear.Text = Convert.ToString(year + i);
            StartYear.Items.Add(addYear);
         
0
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
ID: 21811865
as small as it can get :)
for (int i = 0; i < 6; i++)

    StartYear.Items.Add(DateTime.Now.AddYears(i).Year.ToString());

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21811872
if you are declaring value and text to be the same thing, it is redundant.  Also, if you are declaring them both to be the same, you do not need to declare them as items, you can directly add them as strings.  A couple tricks to make things easier :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Closing Comment

by:fusioninternet
ID: 31468282
Nice one, that did the trick
Thanks for the second reply, I did realise that I would only need to enter it once, but I am planning to have the text as 2008 and the value as 08., would this cause a major upset to the way you've just shown me?

Cheers
Steve
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21813426
to add them in seperately do like pratima said:
            ListItem addYear = new ListItem();

            addYear.Value = Convert.ToString(year + i).SubString(2,2);

            addYear.Text = Convert.ToString(year + i);

            StartYear.Items.Add(addYear);

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21813505
or to keep it small again:
for (int i = 0; i < 6; i++)

  StartYear.Items.Add(new ListItem(DateTime.Now.AddYears(i).Year.ToString(), DateTime.Now.AddYears(i).Year.ToString().Substring(2, 2)));

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

13 Experts available now in Live!

Get 1:1 Help Now