/// <summary> /// Moves all templated controls of a given region provider /// into their target regions. /// </summary> /// <param name="template">The template to be used.</param> /// <param name="controls">Controls to move into the template.</param> /// <param name="provider">Provides region assignements for the controls.</param> /// <param name="page">The currently rendered page.</param> protected static void HandleControls(IPortalTemplate template, RegionProvider provider, ControlCollection controls, Page page) { //force template control creation now by adding / removing the template Control templateControl = (Control)template; controls.Add(templateControl); controls.Remove(templateControl); //call initialization code of the template template.BeforeTemplating(page); //add defined controls to their target region RegionPlaceHolder placeHolder; foreach (RegionPropertySet propertySet in provider) { placeHolder = template[propertySet.TargetRegion]; if (placeHolder == null) { string msg = "Invalid region defined for control {0}. Template does not contain region '{1}'"; msg = String.Format(msg, propertySet.Control.ID, propertySet.TargetRegion); throw new ArgumentException(msg); } //remove templated control from original location... controls.Remove(propertySet.Control); //...and put it into placeholder placeHolder.Controls.Add(propertySet.Control); } //add remaining controls to default region, if any if (provider.DefaultRegion != PortalRegion.None) { placeHolder = template[provider.DefaultRegion]; if (placeHolder == null) { string msg = "Invalid default region defined: Template does not contain region '{0}'"; msg = String.Format(msg, provider.DefaultRegion); throw new ArgumentException(msg); } //move remaining controls into the template's controls ControlUtil.MoveControls(controls, placeHolder.Controls); } //Everything is now in the template. Now move all the template's //controls back into the page's control collection. This is necessary //to keep relative links of the web form working ControlUtil.MoveControls(templateControl.Controls, controls); //call initialization code of the template template.AfterTemplating(page); }
/// <summary> /// Performs templating for a given <see cref="RegionProvider"/>. Use /// this overload if you have a reference to your provider for /// performance reasons. /// </summary> /// <param name="page">The page to be rendered.</param> /// <param name="provider">The region provider that contains /// templating meta data.</param> public static void PerformTemplating(Page page, RegionProvider provider) { HtmlForm form = FindForm(page); if (form == null) { throw new ArgumentException("Could not find a HtmlForm control on that web form which is required by the template engine"); } //load template control try { IPortalTemplate template = (IPortalTemplate)page.LoadControl(provider.RegionTemplatePath); //move controls into template HandleControls(template, provider, form.Controls, page); } catch (InvalidCastException castException) { string msg = "Could not cast the template '{0}'. Make sure that the template derives from '{1}'"; msg = String.Format(msg, provider.RegionTemplatePath, "IPortalTemplate"); throw new ArgumentException(msg, castException); } }