{"id":4041,"date":"2022-12-23T10:33:26","date_gmt":"2022-12-23T10:33:26","guid":{"rendered":"https:\/\/isophal.com\/?p=4041"},"modified":"2022-12-23T10:33:27","modified_gmt":"2022-12-23T10:33:27","slug":"how-to-create-a-dropdownlist-displaying-years-in-asp-net","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2022\/12\/23\/4041.html\/","title":{"rendered":"How to create a DropDownList displaying years in asp.net"},"content":{"rendered":"\n<p>ASP.NET DropDownList Years<\/p>\n\n\n\n<p>The following asp.net c# example code demonstrate us how can we display a list of years in a DropDownList server control. Many web developers want to avoid TextBox to collect a year from web users. To select a year from DropDownList is very easy and error free way. Web visitors can select a year from a drop down list without writing content on TextBox control.<\/p>\n\n\n\n<p>In the bellow example code, we populated a DropDownList with 11 years. We populated the DropDownList with previous 5 years and future 5 years and current year based on the current year. At first we create a variable to hold the current year from system DateTime object. Then we subtract 5 years and Add 5 years with the present year integer value to loop through it. Finally, DropDownList render items with 11 years.<\/p>\n\n\n\n<p><strong>dropdownlist-years.aspx<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\r   &lt;h2 style=\"color:MidnightBlue; font-style:italic;\">      \r\n            ASP.net Example - Dropdownlist Years\r\n        &lt;\/h2>      \r\n        &lt;hr width=\"550\" align=\"left\" color=\"Gainsboro\" \/>      \r\n        &lt;asp:Label       \r\n            ID=\"Label1\"       \r\n            runat=\"server\"      \r\n            Text=\"select an item from dropdownlist.\"\r\n            Font-Size=\"X-Large\"\r\n            Width=\"350\"\r\n            >      \r\n        &lt;\/asp:Label>      \r\n        &lt;br \/>&lt;br \/>\r\n        &lt;asp:DropDownList\r\n            ID=\"DropDownList1\"\r\n            runat=\"server\"\r\n            AutoPostBack=\"true\"\r\n            Width=\"350\"\r\n            Font-Size=\"X-Large\"\r\n            OnSelectedIndexChanged=\"DropDownList1_SelectedIndexChanged\"\r\n            >\r\n        &lt;\/asp:DropDownList>\r\n_____________________________________________\n&lt;script runat=\"server\">   \r\n    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)\r\n    {\r\n        Label1.Text = \"you selected....&lt;br \/>\";\r\n        Label1.Text += DropDownList1.SelectedItem.Text;\r\n    }\r\n&lt;\/script>  \n_____________________________________________\n        protected void Page_Load(object sender, EventArgs e)\r\n        {\r\n            if (!Page.IsPostBack)\r\n            {\r\n                int year = DateTime.Now.Year;\r\n                for (int i = year - 5; i &lt;= year + 5; i++)\r\n                {\r\n                    ListItem li = new ListItem(i.ToString());\r\n                    DropDownList1.Items.Add(li);\r\n                }\r\n                DropDownList1.Items.FindByText(year.ToString()).Selected = true;\r\n            }\r\n\r\n        }\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"428\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2022\/12\/image-36.png\" alt=\"\" class=\"wp-image-4042\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-36.png 706w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-36-600x364.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-36-300x182.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-36-585x355.png 585w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"679\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2022\/12\/image-37-1024x679.png\" alt=\"\" class=\"wp-image-4043\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-1024x679.png 1024w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-600x398.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-300x199.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-768x510.png 768w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-1170x776.png 1170w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-780x516.png 780w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-585x388.png 585w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37-263x175.png 263w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2022\/12\/image-37.png 1254w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET DropDownList Years The following asp.net c#&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4042,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[],"class_list":["post-4041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tipstricks"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=4041"}],"version-history":[{"count":1,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4041\/revisions"}],"predecessor-version":[{"id":4044,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4041\/revisions\/4044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4042"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}