Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

HijriDateView ASP.NET Control

4.91/5 (12 votes)
17 Mar 2009CPOL3 min read 42.8K   1.4K  
An ASP.NET control that allows drawing current Hijri date in string format on an ASP.NET page.

Introduction

Some time ago, working on a project, I faced with the necessity of showing date in Hijri format (Islamic calendar) on an ASP.NET page. Although the FCL contains a HijriCalendar class, it corresponds to an algorithm of calculation only and doesn't allow formatting output for showing date in string format. Therefore, the HijriDateView control, described in this short article, was developed.

How it looks

HijriDateView is an ASP.NET composite control that consists of a Panel (corresponds to an HTML Div) and a plain text inside this Panel. By default, on a page, it looks like:

Since the Panel class is used as a container, any visual style can be applied to the control, to show it like this:

or this:

or this:

How to use the control

Just compile the attached project, or use the already compiled assembly from the IslamicTools_bin.zip file. Reference IslamicTools.dll from your ASP.NET WebForms project and a new item will appear on your Visual Studio's toolbox. Just drag it to the appropriate place on your ASP.NET page, and code similar to this will be generated:
ASP.NET
<IslamicTools:HijriDateView ID="hijriDateView" runat="server">
</IslamicTools:HijriDateView>

Now, you can set some properties of the control:

  • Language - The language that will be used for transliteration of the Arabic month names. Currently, the possible values are English and Russian.
  • IncludeArabic - Indicates whether to show Arabic text in the resulting inscription or not.
  • Date - Date used for conversion and showing. If it is not specified, the current date will be used (DateTime.Now).

All regular ASP.NET control visual properties can also be set here. Now, just compile and run your page to see the result.

How it works

There are the four files related to the control.

  • HijriDate.cs - contains the HijriDate class that envelops the HijriCalendar class from the FCL.
  • HijriDate.xml - contains different transliterations of months including Arabic.
  • HijriDateView.bmp - bitmap for the toolbar.
  • HijriDateView.cs - contains the HijriDateView Web Control that is used on an ASP.NET page.

During the lifecycle of the ASP.NET page, a new instance of the HijriDateView control is created. We set the default values for the Date, Language, and IncludeArabic properties in the constructor. They will be overridden if the user sets their values manually. On the LoadComplete stage of the page's lifecycle, the CreateChildControls method of HijriDateView will be called. It will prepare a container (Panel) and a place for the text (LiteralControl), and at the same time, a new instance of the HijriDate class will be created and used with the defined properties:

C#
protected override void CreateChildControls()
{
  Panel pnl = new Panel();
  HijriDate hd = new HijriDate(dt);
  string label = String.Format("<nobr>{0}</nobr>", 
                               hd.ToString(language, includeArabic));
  LiteralControl lc = new LiteralControl(label);
  Controls.Add(pnl);
  pnl.Controls.Add(lc);
  base.CreateChildControls();
}

The HijriDate class takes a DateTime object as an input. A new instance of HijriCalendar for a specified date is created in the constructor as well as private numeric fields that correspond to year, number of month, and number of days in a month, and are filled out. Also, string recourses are loaded from the HijriDate.xml file.

C#
public HijriDate()
   : this(DateTime.Now)
{
}
public HijriDate(DateTime dt)
{
  hijriCalendar = new HijriCalendar();
  hijriYearNumber = hijriCalendar.GetYear(dt);
  hijriMonthNumber = hijriCalendar.GetMonth(dt);
  hijriDayNumber = hijriCalendar.GetDayOfMonth(dt);
  LoadHijriDateXml();
}

HijriDate.xml has the following format:

XML
<?xml version="1.0" encoding="utf-8" ?>
<Months>
  <month number="1">
    <English>Muharram</English>
    <Arabic>محرم</Arabic>
    <Russian>Мухаррам</Russian>
  </month>
  <month number="2">
    <English>Safar</English>
    <Arabic>صفر</Arabic>
    <Russian>Сафар</Russian>
  </month>
    ...
  <month number="12">
    <English>Dhu al-Hijja</English>
    <Arabic>ذو الحجة</Arabic>
    <Russian>Зуль-Хиджа</Russian>
  </month> 
</Months>

The HijriDate class has an overridden method ToString() that does all the work for converting the numeric representation of a date into a string:

C#
public override string ToString()
{
  return ToString(SupportedLanguages.English, true);
}

public string ToString(SupportedLanguages language, 
                       bool includeArabicInscription)
{
  return String.Format("{0} {1} {2}", hijriDayNumber.ToString(), 
  GetMonthName(language, includeArabicInscription),
  hijriYearNumber.ToString());
}

That’s it. As you might have noticed, not all code is provided here, particularly the methods LoadHijriDateXml() and GetMonthName() are not even described. But their behavior should be obvious from their names, and their code is available in the HijriDateView_src.zip file.

Example

A working control can be seen at http://www.russian-mosques.com. The IslamicTools library contains only a HijriDateView control for now. It is about to be extended with other controls. If you have any ideas or suggestions, you are kindly welcome to contact me.

License

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