Incorrect date format when using SharePoint DateTimeControl in application page launched via ModalDialog

I was working on a project where I needed to create a custom UI for a customer in order to do some complex logic and form formatting. The form was developed as an application page which was deployed into the 14 hive and it was presented to the user via a Popup using the JavaScript ModalDialog functionality. The modal dialog was being launched from a custom web part which was in a subsite below the main site

As part of the form I needed to prompt the user with a field which would capture a date so I used the SharePoint DateTimeControl under the Microsoft.SharePoint.WebControls namespace. I have used this several times before so I didn’t think anything of it until when testing I noticed the date format was US instead of UK. I have had this before so I added some code in the page load of my application page to set the controls regional settings to match the current web regional settings, see below.

Set DateTimeControl Region
  1. DtDueDate.LocaleId = Convert.ToInt32(SPContext.Current.Web.RegionalSettings.LocaleId);

 

This normally works, however when I started testing it was still not setting the region to UK, 2057, instead it was still set to US, 1033. While debugging I noticed the SPContext details where indicating the current web was the top level web not the subsite from which the modal dialog was getting launched.

This explained why the DateTimeControl was displaying in the wrong format as the regional settings at the top level site where set to US. I changed the regional settings at the top level web and the control starting displaying in UK format.

While I was happy this was working I was slightly confused as to why the context was showing the current web to be the root web not the web for the subsite. After some digging around I realised that while I was on the subsite in the main browser window when I was calling the popup I was passing in a relative URL, see examples below, and this meant the context in the popup used this URL not the location from which it was getting called.

Original URL of popup window where context is the root site

“/_layouts/ApplicationPages/Page.aspx”

Adjusted URL of popup window where context is the subsite

“/subsite/_layouts/ApplicationPages/Page.aspx”

This caused me some issues but once I got my head around it it made perfect sense so hopefully this will help others or help me in the future when I forget all about it Smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: