Solved

populate dropdownlist asp.net c# 2.0

Posted on 2007-03-27
10
491 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now