Solved

Dynamically list yers in dropdown

Posted on 2006-06-12
6
282 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

624 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