0

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,

3
Contributors
10
Replies
13
Views
7 Years
Discussion Span
Last Post by dnanetwork
0

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?

0

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..!

0

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.

Edited by mike_2000_17: Fixed formatting

0

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

0
//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.. :)
0

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.

Edited by mike_2000_17: Fixed formatting

0

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?

0

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?
:-/

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.