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>
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;
}
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;
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();
}
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();
}
}
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;
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();
}
}
The above code we can use to create a custom calendar control using C# code.
You may like following SharePoint tutorials:
- Add jQuery in visual web part in SharePoint
- Display tfs work items in GridView using SharePoint Visual Web Part
- How to debug custom web part and timer job in SharePoint 2013 using Visual Studio
- Create a web part page using visual studio 2013 in SharePoint 2013
- Create a custom Team web part using SharePoint server object model and bootstrap
- Site Feed web part in SharePoint 2013/2016/Online
- Add closed web parts on a page in SharePoint
- SharePoint update farm account password
- Update User Email ID using PowerShell in SharePoint
- Create a console application in visual studio code
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.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ 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! 🙂
Thanks. Keep reading my blogs..