Search Asp.Net Here

Show Years in dropDownList




Here is an example how we can generate years in dropdownlist in asp.net at runtime

Add a drop downlist to your page with id DropDownList1
Here is method which will populate dropdownlist


//code
protected void year()
{
for (int i = DateTime.Now.Year; i >= 2000; i--)
{
ListItem obj = new ListItem();
obj.Text = i.ToString();
obj.Value = i.ToString();
DropDownList1.Items.Add(obj);
}
}

call year() method in page load event (.cs)

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
year();
}




Comments :

5 comments to “Show Years in dropDownList”
Anonymous said...
on 

Hi ,
I want to display date in DropDownList and the date is like 2000-2001,2001-2002,2002-2003 etc.
And user select any from it and will display in text box.

Thanks.

no fear choudhary is here said...
on 

you can use the following code

for (int i = DateTime.Now.Year; i >= 2000; i--)
{
ListItem obj = new ListItem();
obj.Text=i.ToString()+"-"+(i+1).ToString();
obj.Value=i.ToString()+"-" +(i + 1).ToString();
DropDownList1.Items.Add(obj);
}

and use the selectedindexchaged event to display selected value in text box

Anonymous said...
on 

Hi Dear,

I want to genearte date in following format and use it in code.
e.g.1011FRP10001
where 1011 is year 2010-2011
FRP is the hard coded string
1 is my dept number
0001 is record number

Could you suggest how 1011FRP10001 will be acheived in asp.net?

My views.......... said...
on 

gud work ..... kwwp it up

Anonymous said...
on 

Hi, I am from Bangladesh. I am a web developer. I have searched many sites but I haven't found anywhere what I was looking for except this blog. Very useful code and helpful guidelines in short. Please keep up the good work.

Post a Comment