?
Solved

Populating dropdownlist in pageload dupliactes items

Posted on 2008-06-18
6
Medium Priority
?
470 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 2000 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
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!

 
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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

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