Dear All,

Please Help Me i want to write a code in asp.net using c# to list previous, current and next 3 years in dropdownlist dynamically at page load without using table.

Thanks and regards,
Dipti

Good Question :-)

You can do this by following code without using any table in Page load event.

DropDownList1.Items.Insert(0, new ListItem(DateTime.Now.AddYears(-1).ToString("yyyy"), DateTime.Now.AddYears(-1).ToString("yyyy"))); // this will show previousyear - 1(2010 - 1 = 2009)


DropDownList1.Items.Insert(1, new ListItem(DateTime.Now.Year.ToString(), DateTime.Now.Year.ToString())); // this will show current year

DropDownList1.Items.Insert(2, new ListItem(DateTime.Now.AddYears(1).ToString("yyyy"), DateTime.Now.AddYears(1).ToString("yyyy"))); // this will show current year + 1 (2010 + 1 = 2011)

DropDownList1.Items.Insert(3, new ListItem(DateTime.Now.AddYears(2).ToString("yyyy"), DateTime.Now.AddYears(2).ToString("yyyy"))); // this will show current year + 2 (2010 + 2 = 2012)


DropDownList1.Items.Insert(4, new ListItem(DateTime.Now.AddYears(3).ToString("yyyy"), DateTime.Now.AddYears(3).ToString("yyyy"))); // this will show current year + 3 (2010 + 3 = 2013)

try by this..hope it will help you...

let us know if you face any trouble...

Dear All,

Please Help Me i want to write a code in asp.net using c# to list previous, current and next 3 years in dropdownlist dynamically at page load without using table.

Thanks and regards,
Dipti

protected void Page_Load(object sender, EventArgs e)
    {
        int i = DateTime.Now.Year ;
        for (i = i - 1; i <= DateTime.Now.Year + 3; i++)
            DropDownList1.Items.Add(Convert.ToString(i));
    }

Use this simple code, it will take current year from your pc.
Best Of Luck.

Thank you to both of you its working.

Please mark the thread Close if it's working as you expect.. So that it will be useful to others.

Thank you to both of you its working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.