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
Medium Priority
543 Views
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
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
• 3
• 3
• 2
• +1

LVL 2

Accepted Solution

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

for (int i = 0; i<=9;i++)
{
}
0

LVL 13

Expert Comment

ID: 18802261
hi,

or

b u d d h a

0

Author Comment

ID: 18802327
protected void PopulateYears()
{
//populate ddlYear
int yr = DateTime.Now.Year;
for (int i = 0; i<=9;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

Author Comment

ID: 18802386
int yr = DateTime.Now.Year;
for (int i = 0; i<=9;i++)
{
}

produces:
20070
20071
etc
20079
0

LVL 13

Assisted Solution

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++)
{
}

b u d d h a
0

LVL 2

Expert Comment

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

Author Comment

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

LVL 2

Expert Comment

ID: 18802838
0

LVL 1

Expert Comment

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++)
{
}
}
}

Greetings,
Nguyen Xuan Huy
0

Featured Post

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
Course of the Month8 days, 18 hours left to enroll