fwsteal
asked on
populate dropdownlist asp.net c# 2.0
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:Lis tItem>
<asp:ListItem Value="2008">2008</asp:Lis tItem>
<asp:ListItem Value="2009">2009</asp:Lis tItem>
<asp:ListItem Value="2010">2010</asp:Lis tItem>
<asp:ListItem Value="2011">2011</asp:Lis tItem>
<asp:ListItem Value="2012">2012</asp:Lis tItem>
<asp:ListItem Value="2013">2013</asp:Lis tItem>
<asp:ListItem Value="2014">2014</asp:Lis tItem>
<asp:ListItem Value="2015">2015</asp:Lis tItem>
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//populate ddlYear
PopulateYears();
}
}
protected void PopulateYears()
{
//how to populate ddlYear
}
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:Lis
<asp:ListItem Value="2008">2008</asp:Lis
<asp:ListItem Value="2009">2009</asp:Lis
<asp:ListItem Value="2010">2010</asp:Lis
<asp:ListItem Value="2011">2011</asp:Lis
<asp:ListItem Value="2012">2012</asp:Lis
<asp:ListItem Value="2013">2013</asp:Lis
<asp:ListItem Value="2014">2014</asp:Lis
<asp:ListItem Value="2015">2015</asp:Lis
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//populate ddlYear
PopulateYears();
}
}
protected void PopulateYears()
{
//how to populate ddlYear
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 .ListItemC ollection. Add(string )' has some invalid arguments.
yr - Argument '1': cannot convert from 'int' to 'string'
{
//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
yr - Argument '1': cannot convert from 'int' to 'string'
ASKER
int yr = DateTime.Now.Year;
for (int i = 0; i<=9;i++)
{
ddlYear.Items.Add((Convert .ToString( yr)) + i);
}
produces:
20070
20071
etc
20079
for (int i = 0; i<=9;i++)
{
ddlYear.Items.Add((Convert
}
produces:
20070
20071
etc
20079
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
fwsteal might you consider splitting points? thx.
ASKER
oops; how do I achieve that? i think I clicked too fast
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.Ite ms.Add(new ListItem((yearStart+i).ToS tring(),(y earStart+i ).ToString ()));
}
}
}
Greetings,
Nguyen Xuan Huy
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.Ite
}
}
}
Greetings,
Nguyen Xuan Huy
ddlYear.Items.Add(new ListItem("2007","2007"));
or
ddlYear.Items.Add("2007");
b u d d h a