Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

populate dropdownlist asp.net c# 2.0

Posted on 2007-03-27
10
Medium Priority
?
543 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

604 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