Solved

Populating dropdownlist in pageload dupliactes items

Posted on 2008-06-18
6
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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