Solved

Populating dropdownlist in pageload dupliactes items

Posted on 2008-06-18
6
468 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nested forach loop to linq 3 41
ASP.NET data base connection 35 81
Convert VB web project to C# 3 59
c# deserialize JSON from web service using JsonConvert 9 50
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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

736 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