Auto-Populate Year and Month in dropdown list in ASP.NET or SharePoint Visual Webpart

This tutorial we will discuss, how to auto-populate Year and Month in a dropdown list using c# code in asp.net or in SharePoint visual web part.

Auto Populate Year and Month in dropdown list in SharePoint

Here I have created four dropdown list which is From Year, From Month To Year and To Month using C#.Net code.

Follow the below source code.

 <div class='FilterRow' runat="server" id="divDateRange">
                            <div class="FormLabel">
                                <label>
                                    <%=GetLocalResourceObject("SelectDateRangeFrom")%></label>
                            </div>
                            <div class="FormControls">
                                <asp:DropDownList runat="server" CssClass="Drop" Style="width: 100px !important;height: 35px !important;" ID="ddlYearFrom" CausesValidation="false" AutoPostBack="true" OnSelectedIndexChanged="ddlYearFrom_SelectedIndexChanged"></asp:DropDownList>
                                <asp:DropDownList runat="server" CssClass="Drop" Style="width: 100px !important;height: 35px !important;" ID="ddlMonthFrom"></asp:DropDownList>
                            </div>
                            <div class="FormLabel">
                                <label>
                                    <%=GetLocalResourceObject("SelectDateRangeTo")%></label>
                            </div>
                            <div class="FormControls">
                                <asp:DropDownList runat="server" CssClass="Drop" Style="width: 100px !important;height: 35px !important;" ID="ddlYearTo" AutoPostBack="true" OnSelectedIndexChanged="ddlYearTo_SelectedIndexChanged"></asp:DropDownList>
                                <asp:DropDownList runat="server" CssClass="Drop" Style="width: 100px !important;height: 35px !important;" ID="ddlMonthTo"></asp:DropDownList>
                            </div>
                        </div>
auto populate Year and Month in dropdown list

Example 1: Getting current Year

Below code we can use to get the current year in the dropdown list.

if (ddlYearFrom.DataSource == null && ddlYearTo.DataSource==null)
            {
                List<int> years = new List<int>();
                for (int i = DateTime.Now.Year; i >= minYear; i--)
                {
                    years.Add(i);
                }
                ddlYearFrom.DataSource = ddlYearTo.DataSource = years;
                ddlYearFrom.DataBind();
                ddlYearTo.DataBind();

                Dictionary<int, string> Frommonth = new Dictionary<int, string>();
                Dictionary<int, string> Tomonth = new Dictionary<int, string>();
                string language = "en-US";
                if (System.Threading.Thread.CurrentThread.CurrentUICulture.LCID == 1025)
                    language = "ar-QA";
                DateTimeFormatInfo datatimeFormat = new CultureInfo(language).DateTimeFormat;
                int minMonth = 1;
                for (int j = DateTime.Now.Month; j >= minMonth; j--)
                {
                    Tomonth.Add(j, datatimeFormat.GetMonthName(j));
                    ddlMonthTo.DataSource = ddlMonthFrom.DataSource = Tomonth;
                    ddlMonthTo.DataTextField = ddlMonthFrom.DataTextField = "Value";
                    ddlMonthTo.DataValueField = ddlMonthFrom.DataValueField = "Key";
                    ddlMonthTo.DataBind();
                    ddlMonthFrom.DataBind();
                }
                int currentYear = DateTime.Now.Year;               ddlYearFrom.Items.FindByValue(currentYear.ToString()).Selected = ddlYearTo.Items.FindByValue(currentYear.ToString()).Selected = true;
            }
Select Year and Month in dropdown list in sharepoint

Example 2: Getting current Month

Below code is to populate the current month in the dropdown list.

int currentMonth = DateTime.Now.Month;
ddlMonthFrom.Items.FindByValue(currentMonth.ToString()).Selected = ddlMonthTo.Items.FindByValue(currentMonth.ToString()).Selected = true;
Select Year and Month in dropdown list in sharepoint 2013

Example 3: Getting all Months

Below is the code to get the months from the drowndown list.

 for (int i = 1; i <= 12; i++)
                {
                    ddlMonthFrom.DataSource = null;
                    StartFrommonth.Add(i, datatimeFormat.GetMonthName(i));
                    ddlMonthFrom.DataSource = StartFrommonth;
                    ddlMonthFrom.DataTextField = "Value";
                    ddlMonthFrom.DataValueField = "Key";
                    ddlMonthFrom.DataBind();
                }
Populate Year and Month in dropdown list in sharepoint visual web part

Example 4: Validate Year and Month

To validate Year and Month we can use the below code in the dropdown list.

 if (Convert.ToInt32(ddlYearFrom.SelectedValue) > Convert.ToInt32(ddlYearTo.SelectedValue))
                                {
                                    lblResult.Text = GetLocalResourceObject("invalidDate").ToString();
                                    lblResult.Visible = true;
                                    return;
                                }
                                else if (Convert.ToInt32(ddlYearFrom.SelectedValue) == Convert.ToInt32(ddlYearTo.SelectedValue))
                                {
                                    if (Convert.ToInt32(ddlMonthFrom.SelectedValue) > Convert.ToInt32(ddlYearTo.SelectedValue))
                                    {
                                        lblResult.Text = GetLocalResourceObject("invalidDate").ToString();
                                        lblResult.Visible = true;
                                        return;
                                    }

                                }

Example 5: Skip the months if Year is 2018

if (ddlYearFrom.SelectedValue == "2018")
            {
                for (int i = 5; i <= 12; i++)
                {
                    ddlMonthFrom.DataSource = null;
                    StartPremonth.Add(i, datatimeFormat.GetMonthName(i));
                    ddlMonthFrom.DataSource = StartPremonth;
                    ddlMonthFrom.DataTextField = "Value";
                    ddlMonthFrom.DataValueField = "Key";
                    ddlMonthFrom.DataBind();
                }
            }
Auto Populate Year and Month in dropdown list in SharePoint

Example 6: Getting Month in both Arabic and English

Below code we can use to get month in different languages in dropdown list.

string language = "en-US";
                if (System.Threading.Thread.CurrentThread.CurrentUICulture.LCID == 1025)
                    language = "ar-QA";
                DateTimeFormatInfo datatimeFormat = new CultureInfo(language).DateTimeFormat;
populate Year and Month in dropdown list in sharepoint visual web part

Example 7: Getting current Month and past month in current Year

Below is the code to get the current and past month in current year in SharePoint visual web part.

 if (Convert.ToInt32(ddlYearFrom.SelectedValue) == DateTime.Now.Year)
            {
                for (int j = DateTime.Now.Month; j >= minMonth; j--)
                {
                    ddlMonthFrom.DataSource = null;
                    StartTomonth.Add(j, datatimeFormat.GetMonthName(j));
                    ddlMonthFrom.DataSource = StartTomonth;
                    ddlMonthFrom.DataTextField = "Value";
                    ddlMonthFrom.DataValueField = "Key";
                    ddlMonthFrom.DataBind();
                }
            }
sharepoint visual web part Select Year and Month in dropdown list

The above code we can use to create a custom calendar control using C# code.

You may like following SharePoint tutorials:

Here we learned how to auto populate Year and Month in a dropdown list using c# code in asp.net or in SharePoint visual web part.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 6 + years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

  • Great content! Super high-quality! Keep it up! 🙂

  • >