?
Solved

populate dropdownlist asp.net c# 2.0

Posted on 2007-03-27
10
Medium Priority
?
540 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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