Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically list yers in dropdown

Posted on 2006-06-12
6
Medium Priority
?
283 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

688 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