/// <summary>
        /// Handles the PreInit event of the page.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void page_PreInit(object sender, EventArgs e)
        {
            //... and change the master page.
            var page = sender as Page;

            // If there's no master page at the moment, this page would probably fail if we assigned a master page as it'll
            // have controls other than <asp:Content /> on it. So just drop out here in that case.
            if (String.IsNullOrEmpty(page.MasterPageFile))
            {
                return;
            }

            var preferredMasterPage = new WebFormsViewSelector().SelectView(page.Request.Url, page.Request.UserAgent);

            // If the master page has been set, change it.
            // Otherwise if no default was in config, just leave it using the one it would've used anyway.
            if (!String.IsNullOrEmpty(preferredMasterPage))
            {
                page.MasterPageFile = preferredMasterPage;
            }
        }
        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            // Ensure controls are visible by default
            this.Visible = true;

            // Look for various reasons why controls may need to be hidden

            // Hide based on master page
            var viewSelector = new WebFormsViewSelector();

            if (Desktop != null && Desktop.Value != viewSelector.CurrentViewIs(Page.MasterPageFile, EsccWebsiteView.Desktop))
            {
                HideContents();
                return;
            }

            if (Plain != null && Plain.Value != viewSelector.CurrentViewIs(Page.MasterPageFile, EsccWebsiteView.Plain))
            {
                HideContents();
                return;
            }

            if (FullScreen != null && FullScreen.Value != viewSelector.CurrentViewIs(Page.MasterPageFile, EsccWebsiteView.FullScreen))
            {
                HideContents();
                return;
            }

            // Hide based on user
            var libraryContext = new LibraryCatalogueContext(HttpContext.Current.Request.UserAgent);

            if (LibraryCatalogue != null && LibraryCatalogue.Value != libraryContext.RequestIsFromLibraryCatalogueMachine())
            {
                HideContents();
                return;
            }

            if (!String.IsNullOrEmpty(Groups))
            {
                var settings    = new ActiveDirectorySettingsFromConfiguration();
                var permissions = new LogonIdentityGroupMembershipChecker(settings.DefaultDomain, new ActiveDirectoryMemoryCache());
                if (!permissions.UserIsInGroup(Groups.SplitAndTrim(';')))
                {
                    HideContents();
                    return;
                }
            }

            // Hide based on location
            if (!String.IsNullOrEmpty(this.UrlMatch) && !Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), this.UrlMatch, RegexOptions.IgnoreCase))
            {
                HideContents();
                return;
            }

            var context = new HostingEnvironmentContext(HttpContext.Current.Request.Url);

            if (Public != null && Public.Value != context.IsPublicUrl)
            {
                HideContents();
                return;
            }

            // Hide based on date
            if (this.After.HasValue && DateTime.Now.ToUkDateTime() <= this.After)
            {
                HideContents();
                return;
            }

            if (this.Before.HasValue && DateTime.Now.ToUkDateTime() >= this.Before)
            {
                HideContents();
                return;
            }
        }