Click here to Skip to main content
15,443,018 members
Articles / Web Development / ASP.NET
Posted 3 Apr 2013


30 bookmarked

ASP.NET DatePicker User Control (Hijri / Gregorian) shows month and year as dropdowns

Rate me:
Please Sign up or sign in to vote.
4.93/5 (39 votes)
6 May 2013CPOL5 min read
ASP.NET Date Selector User Control based on calendar control.

Image 1

You can try live demo here.


I was developing an application that uses the date picker for date fields like date of birth and expiry date. Some fields should be entered in Hijri or Gregorian because here in Saudi Arabia the official calendar is the Hijri Calendar and many people know their dates in Hijri only. So I need a date picker control to allow the user to choose the calendar (Hijri or Gregorian) and finally fill both dates in textboxes.

I tried to achieve the above goals be developing this user control. Hopefully it will be useful for anyone interested in Hijri date picker.

Later on I faced some delay issues because of server side postback when you toggle between Hijri and Gregorian, I have developed another control working fully on client side using javascript in new article (Date User Control Support Hijri and Gregorian Based on Javascript).

What is wrong with Ajax Toolkit Datepicker control?

I was looking for an AJAX or jQuery date picker that supports Hijri and Gregorian. Also show month and year as drop downs instead of the static month/year header to facilitate navigation through a large time frame. Unfortunately I did not find any control supporting Hijri, and the Ajax control toolkit date picker here has a bug in Hijri year navigation.

Introduction to the Islamic (Hijri) Calendar

The Islamic calendar, Muslim calendar, or Hijri calendar (AH) is a lunar calendar consisting of 12 months in a year of 354 or 355 days. Being a purely lunar calendar, it is not synchronized with the seasons. With an annual drift of 10 or 11 days, the seasonal relation repeats about every 33 Islamic years (every 32 solar years).

It is used to date events in many Muslim countries (concurrently with the Gregorian calendar), and is used by Muslims everywhere to determine the proper days on which to observe the annual fast (Ramadan), to attend Hajj, and to celebrate other Islamic holidays and festivals.

The first year was the Islamic year beginning in AD 622 during which the emigration of prophet Muhammad, peace be upon him, from Mecca to Medina, known as the Hijra, occurred. Each numbered year is designated either H for Hijra or AH for the Latin anno Hegirae (in the year of the Hijra) [3] hence Muslims typically call their calendar the Hijri calendar.

The current Islamic year is 1434 AH. In the Gregorian calendar 1434 AH runs from approximately 14 November 2012 to 4 November 2013.

Using the Code 

Simply drag and drop the user control into your ASPX page, add ScriptManger to your ASPX page, and calendar image to your solution. More information about including the user control in an ASP.NET web Page.

Image 2

User Control Properties

The control exposes six properties: DefaultCalendarCulture, SelectedCalendareDate, getHijriDateText, getGregorianDateText, MinYearCountFromNow and MaxYearCountFromNow that can be used in the hosting page.

Expose default calendar culture and show the little Intellisense box pop up options (Higri, Gregorian).


//To set the default Calendar to Hijri 
<uc1:HijriGregDatePicker ID="HijriGregDatePicker1" 
  runat="server"  DefaultCalendarCulture="Arabic" >

//To set the default Calendar to Gregorian from Source
<uc1:HijriGregDatePicker ID="HijriGregDatePicker2" 
  runat="server"  DefaultCalendarCulture="English" >

//To set the default Calendar to Hijri from Code
HijriGregDatePicker1.DefaultCalendarCulture =DatePicker.DefaultCultureOption.Arabic;

//To set the default Calendar to Gregorian from Code
HijriGregDatePicker1.DefaultCalendarCulture =DatePicker.DefaultCultureOption.English;

//Note that if you have multiple instances from the userconrtrol
//you have to give all of instances the same default culture
//otherwise you will got inconvenient behaviour

You can get the Hijri selected date:

   new CultureInfo("ar-SA").DateTimeFormat); 

You can get the Gregorian selected date:

   new CultureInfo("en-US").DateTimeFormat);

You can set the date from the database:

if (!IsPostBack)
    DataView dvDataView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    HijriGregDatePicker1.SelectedCalendareDate = (DateTime)dvDataView[0][0];   

To get the Hijri date from the Hijri text box:

string strHijri = HijriGregDatePicker1.getHijriDateText; 

To get the Gregorian date from the Gregorian text box:

string strGreg = HijriGregDatePicker1.getGregorianDateText;

To set the minimum year allowed to be selected in the year dropdwon list:

//To set the minimum year to 2003
HijriGregDatePicker1.MinYearCountFromNow = -10;

To set the maximum year allowed to be selected on year dropdwon list:

//to set the maximum year to 2023
HijriGregDatePicker1.MaxYearCountFromNow = 10;
  1. DefaultCalendarCulture

  2. SelectedCalendareDate

  3. getHijriDateText

  4. getGregorianDateText

  5. MinYearCountFromNow

  6. MaxYearCountFromNow

The Solution

I depended on the ASPX calendar control to develop this solution and the main problem was that the calendar control loaded according to the page culture and there was no way to change the calendar culture without changing the page culture.

I created a usercontrol for this matter to behave like a date picker, toggling between cultures using a dropdown list that changed the page culture. Also I put this user control inside an UpdatePanel to partially update the calendar without affecting the current page.

In other words I can say since we cannot set two different cultures in a single page, this solution is a workaround to allow the user to use multiple calendars in the same page.

Multiple Instances from the User Control in the Same Page

It was a big challenge for me to allow multiple instances. Because I have to manage the post back and culture changing for all instances. For example if the post back is triggered from the culture dropdown list, year dropdown list, or month dropdown list, the calendar div will stay visible but if triggered by other controls the calendar div status will be changed to hidden.

//To get the potpack control name
private string getPostBackControlName()
    Control control = null;
    //first we will check the "__EVENTTARGET" because if post back made by the controls
    //which used "_doPostBack" function also available in Request.Form collection.
    string ctrlname = Page.Request.Params["__EVENTTARGET"];
    if (ctrlname != null && ctrlname != String.Empty)
        control = Page.FindControl(ctrlname);

    if (control == null)
        return string.Empty;
        //to catch the control name in case of multiple instances
        return control.UniqueID;

User Control JavaScript

The JavaScript for this user control is very simple as below:

//To Show hide the div when user click on calendar image or any date text boxes
<script type="text/javascript">
    function showHide(div) {
        if (document.getElementById(div).style.display == "none") {
            document.getElementById(div).style.display = "block";
        else { document.getElementById(div).style.display = "none"; }
//to hide the date picker when user click outside the date picker box
<script type="text/javascript">
document.onclick = function (e) {
    e = e || event
    var target = || e.srcElement
    var box = document.getElementById('<% =this.whole_calendar.ClientID %>')
    var imgCal = document.getElementById('<% =this.imgCalendar.ClientID %>')
    var txtHijri = document.getElementById('<% =this.txtHijri.ClientID %>')
    var txtGreg = document.getElementById('<% =this.txtGreg.ClientID %>')
    do {
        if (box == target | imgCal == target | txtHijri == target | txtGreg == target) {
            // Click occured inside the box, do nothing.
        target = target.parentNode
    while (target)
    // Click was outside the box, hide it. = "none"
  1. Show/hide the calendar date picker
  2. Hiding the date picker when the user clickd on any place outside date picker box

Using Validation Control

You can use Validation controls to validate the selected date, for example, I have used the custom validator to check the selected date should be between 01/01/2015 and 12/31/2016.

<script type="text/javascript">
// to validate date between two dates note that date format MM/dd/yyyy
function checkDate(source, arguments) {
    var minDate = new Date('01/01/2015');
    var maxDate = new Date('12/31/2016');
    var selTxtDate = document.getElementById('<% =this.HijriGregDatePicker1.FindControl("txtGreg").ClientID %>').value;
    var CurrentDate = new Date(selTxtDate.substr(3, 2) + "/" + selTxtDate.substr(0, 2) + "/" + selTxtDate.substr(6, 4));
    if (CurrentDate - minDate > 0 && CurrentDate - maxDate < 0)
        arguments.IsValid = true;
        arguments.IsValid = false;
<asp:CustomValidator ID="dateCustomvalidator" runat="server" ControlToValidate="HijriGregDatePicker1:txtGreg" ClientValidationFunction="checkDate" Display="Dynamic" SetFocusOnError="True" ErrorMessage="The date shoud be between 01/01/2015 and 12/31/2016" ForeColor="Red"  />    

Points of Interest

Note that when you change the date picker culture (Hijri to Gregorian or vice versa), you change the page culture also. Therefore if you are using resource files (localization) I suggest you save the page culture in the hidden field when the page loads and retrieve it back when submitting, as below:

protected void Page_Load(object sender, EventArgs e)
    hidCulture.Value = CultureInfo.CurrentCulture.ToString();

protected void btnSubmit_Click(object sender, EventArgs e)
    System.Globalization.CultureInfo culture = 
    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;


Note that you if you want to use the calendar for single culture only, then you can disable the dropdown list for changing culture. But if you have many cultures in your application, you cannot enforce the user to choose a date in a specific culture only, because the other user control instances will be affected and any change in page culture will affect the calendar.

Browser Compatibility

This control has been tested on the latest version of Firefox, Internet Explorer, Chrome and Safari


I tried my best to make this user control free of bugs. Any comments, ideas, and suggestions are most welcome for further improvement in this user control.



  • Version 1.0: 31/03/2013.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionAge Calculator between two hijri dates Pin
$ultaNn20-Dec-21 20:03
Member$ultaNn20-Dec-21 20:03 
AnswerRe: Age Calculator between two hijri dates Pin
Yahya Mohammed Ammouri20-Dec-21 21:02
MemberYahya Mohammed Ammouri20-Dec-21 21:02 
QuestionMs Access Pin
Zaboor21-Oct-21 10:18
MemberZaboor21-Oct-21 10:18 
AnswerRe: Ms Access Pin
Yahya Mohammed Ammouri16-Nov-21 23:29
MemberYahya Mohammed Ammouri16-Nov-21 23:29 
Questionمحتاج التقويم على لغة php Pin
Member 149736754-Mar-21 9:11
MemberMember 149736754-Mar-21 9:11 
AnswerRe: محتاج التقويم على لغة php Pin
Yahya Mohammed Ammouri16-Nov-21 23:28
MemberYahya Mohammed Ammouri16-Nov-21 23:28 
Questionهل ممكن تحويل الكود الى php Pin
Member 1491771623-Aug-20 21:45
MemberMember 1491771623-Aug-20 21:45 
AnswerRe: هل ممكن تحويل الكود الى php Pin
Yahya Mohammed Ammouri16-Nov-21 23:27
MemberYahya Mohammed Ammouri16-Nov-21 23:27 
QuestionRevert to Gregorian calendar Pin
$ultaNn22-Mar-18 9:25
Member$ultaNn22-Mar-18 9:25 
It works perfectly fine
i want to revert the culture of my page to English as my other calender's is also converting to hijri when i select hijri date .
AnswerRe: Revert to Gregorian calendar Pin
Yahya Mohammed Ammouri4-Jul-18 20:05
MemberYahya Mohammed Ammouri4-Jul-18 20:05 
QuestionThank You Pin
MohammedSadik21-Jan-18 0:18
professionalMohammedSadik21-Jan-18 0:18 
AnswerRe: Thank You Pin
Yahya Mohammed Ammouri4-Jul-18 20:01
MemberYahya Mohammed Ammouri4-Jul-18 20:01 
QuestionException while using masterpage and update panel Pin
jebinthilak20-Oct-17 20:46
Memberjebinthilak20-Oct-17 20:46 
AnswerRe: Exception while using masterpage and update panel Pin
jebinthilak23-Oct-17 2:42
Memberjebinthilak23-Oct-17 2:42 
GeneralRe: Exception while using masterpage and update panel Pin
Yahya Mohammed Ammouri23-Oct-17 3:11
MemberYahya Mohammed Ammouri23-Oct-17 3:11 
Questionadd this datpicker in ironspeed application as third party Pin
bassemyahya14-Aug-17 2:36
Memberbassemyahya14-Aug-17 2:36 
AnswerRe: add this datpicker in ironspeed application as third party Pin
Yahya Mohammed Ammouri19-Aug-17 19:05
MemberYahya Mohammed Ammouri19-Aug-17 19:05 
Questionmake select event public Pin
maher khalil15-Dec-16 21:12
Membermaher khalil15-Dec-16 21:12 
AnswerRe: make select event public Pin
jebinthilak20-Oct-17 21:04
Memberjebinthilak20-Oct-17 21:04 
Questionin Pin
Member 1150876814-Dec-16 22:04
MemberMember 1150876814-Dec-16 22:04 
QuestionUmmul Quara calendar Pin
Member 120541163-Nov-16 3:43
MemberMember 120541163-Nov-16 3:43 
AnswerRe: Ummul Quara calendar Pin
Yahya Mohammed Ammouri4-Nov-16 1:24
MemberYahya Mohammed Ammouri4-Nov-16 1:24 
QuestionAdd the user control in DetailsView Pin
Member 1253168628-May-16 22:49
MemberMember 1253168628-May-16 22:49 
AnswerRe: Add the user control in DetailsView Pin
Yahya Mohammed Ammouri31-May-16 19:01
MemberYahya Mohammed Ammouri31-May-16 19:01 
Praisegood job Pin
EgnYossef21-Feb-16 0:58
MemberEgnYossef21-Feb-16 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.