Solved

Dynamically list yers in dropdown

Posted on 2006-06-12
6
279 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:claracruz
[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
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16886733
           int year = DateTime.Now.Year;

            for ( int i = 0; i < 10; i++ )
            {
                comboBox1.Items.Add(year.ToString());
                ++year;
            }
0
 
LVL 9

Accepted Solution

by:
msdixon earned 250 total points
ID: 16889950
alex,
why are you using ++year ? there's really no reason for that.

this is functionally equivalent:
           int year = DateTime.Now.Year;

            for ( int i = 0; i < 10; i++ )
            {
                comboBox1.Items.Add(year++.ToString());
            }

or, if you want to exclude this year use:
           int year = DateTime.Now.Year;

            for ( int i = 0; i < 10; i++ )
            {
                comboBox1.Items.Add((++year).ToString());
            }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16892019
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:claracruz
ID: 16892418
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
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 16892503
Code fragments shown in previous posts are for Windows Forms. For ASP you need to insert server-side script which basically does the same. My guess is:

<asp:DropDownList ID="year" runat="server" CssClass="input" Width="140px">
                <asp:ListItem Text=" ----- Any Year ------ "/>
                <%year = DateTime.Now.Year;
                     for ( i = 0; i < 10; i++ )
                     {%>
                          <asp:ListItem Text="<%year++.ToString()%>"/>
                <%}%>
              </asp:DropDownList>

I am not sure that this is right, my ASP experience is from year 2000, hopefully somebody can correct this.
0
 
LVL 4

Author Comment

by:claracruz
ID: 16929265
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();
        }

      }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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