Solved

Dynamically list yers in dropdown

Posted on 2006-06-12
6
276 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now