Solved

populate dropdownlist asp.net c# 2.0

Posted on 2007-03-27
10
532 Views
Last Modified: 2007-12-19
I need to populate the following drop down list from the
code behind with year values that start at current year and
go up to 9 years in future.

<asp:DropDownList ID="ddlYear" runat="server" TabIndex="20">
  <asp:ListItem Value="2007">2007</asp:ListItem>
  <asp:ListItem Value="2008">2008</asp:ListItem>
  <asp:ListItem Value="2009">2009</asp:ListItem>
  <asp:ListItem Value="2010">2010</asp:ListItem>
  <asp:ListItem Value="2011">2011</asp:ListItem>
  <asp:ListItem Value="2012">2012</asp:ListItem>
  <asp:ListItem Value="2013">2013</asp:ListItem>
  <asp:ListItem Value="2014">2014</asp:ListItem>
  <asp:ListItem Value="2015">2015</asp:ListItem>                
</asp:DropDownList>


protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
   {
    //populate ddlYear
    PopulateYears();
   }
 }

protected void PopulateYears()
{
  //how to populate ddlYear

}
0
Comment
Question by:fwsteal
[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
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Accepted Solution

by:
alfredwhang earned 250 total points
ID: 18802099
int yr = Now().Year()

for (int i = 0; i<=9;i++)
{
  ddlYear.Items.Add(yr+i)
}
0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 18802261
hi,

ddlYear.Items.Add(new ListItem("2007","2007"));
or
ddlYear.Items.Add("2007");

b u d d h a

0
 

Author Comment

by:fwsteal
ID: 18802327
   protected void PopulateYears()
    {
        //populate ddlYear
        int yr = DateTime.Now.Year;
        for (int i = 0; i<=9;i++)
            {
                ddlYear.Items.Add(yr + i);
            }
    }

ddlYear - The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Add(string)' has some invalid arguments.

yr - Argument '1': cannot convert from 'int' to 'string'
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:fwsteal
ID: 18802386
       int yr = DateTime.Now.Year;
        for (int i = 0; i<=9;i++)
            {
                ddlYear.Items.Add((Convert.ToString(yr)) + i);
            }

produces:
20070
20071
etc
20079
0
 
LVL 13

Assisted Solution

by:jeebukarthikeyan
jeebukarthikeyan earned 250 total points
ID: 18802437
hi,

change the code likes this

hi,

int yr = DateTime.Now.Year;
for (int i = 0; i<=9;i++)
{
  ddlYear.Items.Add((Convert.ToString(yr+ i)) );
}


b u d d h a
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802633
fwsteal might you consider splitting points? thx.
0
 

Author Comment

by:fwsteal
ID: 18802692
oops; how do I achieve that? i think I clicked too fast
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802838
0
 
LVL 1

Expert Comment

by:google-web-info
ID: 18806323
This is a solution for you:
protected void Page_Load(object sender, EventArgs e)
        {
            int yearStart = DateTime.Now.Year;
            if(!Page.IsPostBack)
            {
                for(int i=0;i<9;i++)
                {
                    this.Year_DropDownList.Items.Add(new ListItem((yearStart+i).ToString(),(yearStart+i).ToString()));
                }
            }
        }

Greetings,
Nguyen Xuan Huy
0

Featured Post

Industry Leaders: 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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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