ASP.NET list item not working

I am not a web developer but I have a good understanding of how certain things work but I do not understand this particular problem:

I have a site that has a dropdown list for picking dates. I want to add the dates 2001-2010 onto the code below but cannot seem to get it to function.


 <asp:DropDownList ID="ddlYear" runat="server" Height="21px" Width="56px">			
                        <asp:ListItem>Year</asp:ListItem>
                        <asp:ListItem>2000</asp:ListItem>
                        <asp:ListItem>1999</asp:ListItem>
                        <asp:ListItem>1998</asp:ListItem>
                        <asp:ListItem>1997</asp:ListItem>
                        <asp:ListItem>1996</asp:ListItem>
                        <asp:ListItem>1995</asp:ListItem>
                        <asp:ListItem>1994</asp:ListItem>
                        <asp:ListItem>1993</asp:ListItem>
                        <asp:ListItem>1992</asp:ListItem>
                        <asp:ListItem>1991</asp:ListItem>
                        <asp:ListItem>1990</asp:ListItem>
                        <asp:ListItem>1989</asp:ListItem>
                        <asp:ListItem>1988</asp:ListItem>
                        <asp:ListItem>1987</asp:ListItem>
                        <asp:ListItem>1986</asp:ListItem>
                        <asp:ListItem>1985</asp:ListItem>
                        <asp:ListItem>1984</asp:ListItem>
                        <asp:ListItem>1983</asp:ListItem>
                        <asp:ListItem>1982</asp:ListItem>
                        <asp:ListItem>1981</asp:ListItem>
                        <asp:ListItem>1980</asp:ListItem>
                        <asp:ListItem>1979</asp:ListItem>
                        <asp:ListItem>1978</asp:ListItem>
                        <asp:ListItem>1977</asp:ListItem>
                        <asp:ListItem>1976</asp:ListItem>
                        <asp:ListItem>1975</asp:ListItem>
                        <asp:ListItem>1974</asp:ListItem>
                        <asp:ListItem>1973</asp:ListItem>
                        <asp:ListItem>1972</asp:ListItem>
                        <asp:ListItem>1971</asp:ListItem>

Open in new window




I added the entry below to the asp.net file yet it does not appear. I have the source code and tried to rebuild the whole thing and it still does not show up. Can anyone help me?

 
  <asp:ListItem>2010</asp:ListItem>
                        <asp:ListItem>2009</asp:ListItem>
                        <asp:ListItem>2008</asp:ListItem>
                        <asp:ListItem>2007</asp:ListItem>
                        <asp:ListItem>2006</asp:ListItem>
                        <asp:ListItem>2005</asp:ListItem>
                        <asp:ListItem>2004</asp:ListItem>
                        <asp:ListItem>2003</asp:ListItem>
                        <asp:ListItem>2002</asp:ListItem>
                        <asp:ListItem>2001</asp:ListItem>

Open in new window

MSSC_supportAsked:
Who is Participating?
 
Lokesh B RDeveloperCommented:
Hi,

There are 2 ways of doing it.

1. Static Values in ASPX.

<asp:DropDownList ID="ddlYear" runat="server" Height="21px" Width="56px">
            <asp:ListItem>Year</asp:ListItem>
            <asp:ListItem>2010</asp:ListItem>
            <asp:ListItem>2009</asp:ListItem>
            <asp:ListItem>2008</asp:ListItem>
            <asp:ListItem>2007</asp:ListItem>
            <asp:ListItem>2006</asp:ListItem>
            <asp:ListItem>2005</asp:ListItem>
            <asp:ListItem>2004</asp:ListItem>
            <asp:ListItem>2003</asp:ListItem>
            <asp:ListItem>2002</asp:ListItem>
            <asp:ListItem>2001</asp:ListItem>
            <asp:ListItem>2000</asp:ListItem>
            <asp:ListItem>1999</asp:ListItem>
            <asp:ListItem>1998</asp:ListItem>
            <asp:ListItem>1997</asp:ListItem>
            <asp:ListItem>1996</asp:ListItem>
            <asp:ListItem>1995</asp:ListItem>
            <asp:ListItem>1994</asp:ListItem>
            <asp:ListItem>1993</asp:ListItem>
            <asp:ListItem>1992</asp:ListItem>
            <asp:ListItem>1991</asp:ListItem>
            <asp:ListItem>1990</asp:ListItem>
            <asp:ListItem>1989</asp:ListItem>
            <asp:ListItem>1988</asp:ListItem>
            <asp:ListItem>1987</asp:ListItem>
            <asp:ListItem>1986</asp:ListItem>
            <asp:ListItem>1985</asp:ListItem>
            <asp:ListItem>1984</asp:ListItem>
            <asp:ListItem>1983</asp:ListItem>
            <asp:ListItem>1982</asp:ListItem>
            <asp:ListItem>1981</asp:ListItem>
            <asp:ListItem>1980</asp:ListItem>
            <asp:ListItem>1979</asp:ListItem>
            <asp:ListItem>1978</asp:ListItem>
            <asp:ListItem>1977</asp:ListItem>
            <asp:ListItem>1976</asp:ListItem>
            <asp:ListItem>1975</asp:ListItem>
            <asp:ListItem>1974</asp:ListItem>
            <asp:ListItem>1973</asp:ListItem>
            <asp:ListItem>1972</asp:ListItem>
            <asp:ListItem>1971</asp:ListItem>
        </asp:DropDownList>

Open in new window


2. Adding the Values Dynamically.

<asp:DropDownList ID="ddlYear" runat="server" Height="21px" Width="56px">
        </asp:DropDownList>

Open in new window


In Page_Load add the following

 
protected void Page_Load(object sender, EventArgs e)
        {
            List<int> years = new List<int>();
            for (int i = 1971; i <= 2010; i++)
            {
                years.Add(i);
            }

            ddlYear.DataSource = years.OrderByDescending(n => n);
            ddlYear.DataBind();
            ddlYear.Items.Insert(0, new ListItem() { Text = "Year" });
}

Open in new window

0
 
kblauCommented:
Do this in MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EEDropdownDates.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            List<SelectListItem> yrs = new List<SelectListItem>();
            for (int i = 1971; i < 2011; i++)
            {
                var newItem = new SelectListItem { Text = i.ToString(), Value = i.ToString() };
                yrs.Add(newItem);
            }
           
            return View(yrs);
        }

    }
}

@model IEnumerable<SelectListItem>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.DropDownList("yrs",Model)
0
 
MSSC_supportAuthor Commented:
Thank you very much. This works as it should.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.