c# - how to add year in drop down from past 2 year till present year in asp.net -


i tried sorts of code this,but failed. please me in filling years in drop down. thank in advance.

    dropdownlist2.items.clear();     dropdownlist2.items.add("--year--");     (int = 3;i>=0 ; i--)    {        label1.text = datetime.now.year . tostring();        //datetimeformatinfo dinfo = new datetimeformatinfo();        int year= int .parse ( label1. text);        label1.text = dinfo.getmonthname(year);         int year= datetime.now.year;         label1.text = year.tostring();                 dropdownlist2.items.add(year+i+ " ");    } 

let's start simplifying code. part fine:

dropdownlist2.items.clear(); dropdownlist2.items.add("--year--"); 

but there several things wrong attempt. first 3 down 0 stepping 1 give previous three years. can change for-loop step 2 years ago, 1 year ago, now. label1 doesn't seem serve purpose, let's eliminate well. you're left few lines current year, loop 3 iterations, , calculate new year (converted string) based on counter in loop, added list:

var currentyear = datetime.today.year; (int = 2; >= 0; i--) {         // add entry that's current year minus counter     dropdownlist2.items.add((currentyear - i).tostring()); } 

so, with:

var currentyear = datetime.today.year; // 2014 

let's loop:

  • when i == 2, (currentyear - i).tostring() "2012"
  • when i == 1, (currentyear - i).tostring() "2013"
  • when i == 0, (currentyear - i).tostring() "2014"

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -