27 February 2014

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();
}

No comments:

Post a Comment