Hello guys,

i have my drop down list in which i have 5 years from 2010-2006.by default start year is current year and last year is current year-5.

exa:- if start year is 2010 then last year is 2006 now if 2011 begins
then start year will be 2011 and last year is 2007,if there is 2012 begins then start year is 2012 and last year will be 2008, so on....

now problem is that how can i auto update years in my dropdownlist when new years begins?please give me code for the same.

Happy Mother's Day,

you just apply this code

int year=DateTime.now.year;
int lastyear=(year-5);
ListItem[] li=new ListItem[5];
for(int i=0;i<5;i++)
{
li=new ListItem(lastyear++);
}
ddl.DataSource=li;
ddl.DataBind();

Apply This logic according your requirement.....
I think this help?

on page load you can use chirag's idea..

on selected index change you need clear the dropdown and rebind the dropdown depend on the current selection of year...

hope that's wot u lokin 4..!

thank you chiragsathit,
but i got red line(error) at li[i] = new ListItem(lastyear++); though i have not run it.the code is below.mention i have written code in ispostback.is it wrtie?? give me guidence.

if (!IsPostBack)
        {
            int year = DateTime.Now.Year;
            int lastyear = (year - 5);
            ListItem[] li = new ListItem[5];
            for (int i = 0; i < 5; i++)
            {
                li[i] = new ListItem(lastyear++) ;

            }
            ddlSelectYear.DataSource = li;
            ddlSelectYear.DataBind();

            DisplayNews();
        }

i m waiting for ur reply as soon as possible.

Thank u dnanetwork for replying me,
i have applied chirag's code in page load as well as in IsPostBack but i got red line(error) at li = new ListItem(lastyear++) ; i have nt run code as i got red line(error).

kindly give me some guidence for the same as soon as possible.
waiting for ur reply dear.
thank you

//Code Behind..

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            for (int i = 2005; i < 2011; i++)
                DropDownList1.Items.Add(i.ToString());
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int currentYear = int.Parse(DropDownList1.SelectedItem.Text.ToString());

        DropDownList1.Items.Clear();

        for(int i =currentYear; i < currentYear + 6; i++)
            DropDownList1.Items.Add(i.ToString());
    }

// try this code and let me know the o/p..
// there is always a way.. :)

thank you dnanetwork,
i have used my code below and by this code i got output.but by default there is 2010 in ddl and in ddl list is from 2006-2010 in desc order.i want ddl list in asc order from 2010-2006.

if (!IsPostBack)
        {
            int year = DateTime.Now.Year;
            int lastyear = (year - 4);
            ArrayList g = new ArrayList();


            for (int i = year  ; i >=lastyear   ; i--)
            {
                g.Add(i);

            }
            ddlSelectYear.DataSource = g;
            ddlSelectYear.DataBind();
}

pl answer me as soon as possible.

did u try my code..?

it works parallel in same sense..

ya but i have tried in my sense.and thanks for the help.

next problem is when page load in ddl there is 2010 by default but in ddl list it shows in desc order from 2006-2010.how can i do that in asc order?

ya but i have tried in my sense.and thanks for the help.

next problem is when page load in ddl there is 2010 by default but in ddl list it shows in desc order from 2006-2010.how can i do that in asc order from 2010-2006in ddlist?
:-/

grab in arraylist..

arraylist.sort();

you gotta chill man! :)

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.