Solved

Populating dropdownlist in pageload dupliactes items

Posted on 2008-06-18
6
466 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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