Link to home
Start Free TrialLog in
Avatar of claracruz
claracruz

asked on

Dynamically list yers in dropdown

I need to generate a dropdownlist dynamically. the dropdown list whould have the list of next 10 yrs. like "2003","2004","2005","2006","2007","2008","2009","2010","2011","2012"
the years are based on current yr.(start from)

Anyone can guide me on this?

Also is there anywhere I can do this and use the ObjectDataSource to populate the list

Thanks
Avatar of AlexFM
AlexFM

           int year = DateTime.Now.Year;

            for ( int i = 0; i < 10; i++ )
            {
                comboBox1.Items.Add(year.ToString());
                ++year;
            }
ASKER CERTIFIED SOLUTION
Avatar of msdixon
msdixon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I like to write by such way for readability. No problem, if you want to save code line, in any case this produces the same executable code.
Avatar of claracruz

ASKER

Thanks for that guus,

As do I apply your above code to the following;-

<asp:DropDownList ID="year" runat="server" CssClass="input" Width="140px">
                <asp:ListItem Text=" ----- Any Year ------ "/>
                <asp:ListItem Text="2006"/>
                <asp:ListItem Text="2005"/>
                <asp:ListItem Text="2004"/>
                <asp:ListItem Text="2003"/>
              </asp:DropDownList>

bearing in mind am trying to seperate code from presentation
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the bit of code, I used it to bind to the dropdown from codebehind link so;-

<asp:DropDownList ID="ddyear" runat="server" CssClass="input" Width="140px"></asp:DropDownList>

Then in my codebehind;-

 protected void loadPage(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
            String[] Array1 = new string[20];
            int nIntial = 2006;
            Array1[0] = " ---- Any Year ---- ";
            for (int i = 1; i < Array1.Length; i++)
            {
                Array1[i] = (nIntial--) + "";
            }
            ddyear.DataSource = Array1;
            ddyear.DataBind();
        }

      }