Click here to Skip to main content
15,867,756 members
Articles / Web Development / ASP.NET

HijriDateView ASP.NET Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (12 votes)
17 Mar 2009CPOL3 min read 41.7K   1.4K   23   10
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)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Work: HSBC (http://www.hsbc.co.uk/).
Regalia: PhD in CS, MCAD, MCPD: Web Developer, MCTS: .Net Framework 2.0., 3.5.
Interests: Programming, artificial intelligence, C#, .NET, HTML5, ASP.NET, SQL, LINQ.
Marital Status: Married, daughter
Blog: http://www.magomedov.co.uk

Comments and Discussions

 
Generalgreat job thanks Pin
Mohamed Abdullah 8222-Mar-09 23:39
Mohamed Abdullah 8222-Mar-09 23:39 
jazak allah khairan my bro it's a great work thanks
GeneralRe: great job thanks Pin
Bashir Magomedov23-Mar-09 1:31
Bashir Magomedov23-Mar-09 1:31 
GeneralExcellent Pin
Muneeb R. Baig20-Mar-09 1:35
Muneeb R. Baig20-Mar-09 1:35 
GeneralRe: Excellent Pin
Bashir Magomedov20-Mar-09 7:23
Bashir Magomedov20-Mar-09 7:23 
GeneralRe: Excellent Pin
Muneeb R. Baig20-Mar-09 10:20
Muneeb R. Baig20-Mar-09 10:20 
GeneralRe: Excellent Pin
Bashir Magomedov20-Mar-09 10:23
Bashir Magomedov20-Mar-09 10:23 
GeneralHijriDateView ASP.NET Control Pin
samerh17-Mar-09 19:55
samerh17-Mar-09 19:55 
GeneralRe: HijriDateView ASP.NET Control Pin
Bashir Magomedov17-Mar-09 20:05
Bashir Magomedov17-Mar-09 20:05 
GeneralRe: HijriDateView ASP.NET Control Pin
samerh17-Mar-09 20:27
samerh17-Mar-09 20:27 
GeneralRe: HijriDateView ASP.NET Control Pin
Bashir Magomedov18-Mar-09 6:07
Bashir Magomedov18-Mar-09 6:07 

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.