Themeable class for displaying WidgetBase derived controls.
WidgetContainer is meant to be the themeable parent class of any control that derives from WidgetBase. This way a theme can automatically apply some basic styling to the way widgets are displayed without having to edit each one or edit the WidgetBase class to change the rendered output. Inherited WidgetContainers must contain a control named phWidgetBody. This is the control that the WidgetContainer's child Widget is injected inside of. phWidgetBody just needs to derive from Control to work, leaving flexibility for anyone creating a theme. If phWidgetBody isn't found, an exception isn't thrown, but a warning label is applied to the page.
Inheritance: System.Web.UI.UserControl
Example #1
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
        /// </summary>
        /// <param name="e">
        /// The <see cref="T:System.EventArgs"></see> object that contains the event data.
        /// </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var zone = this.XmlDocument.SelectNodes("//widget");

            if (zone == null)
            {
                return;
            }

            // This is for compatibility with older themes that do not have a WidgetContainer control.
            var widgetContainerExists      = WidgetContainer.DoesThemeWidgetContainerExist();
            var widgetContainerVirtualPath = WidgetContainer.GetThemeWidgetContainerVirtualPath();

            foreach (XmlNode widget in zone)
            {
                var fileName = string.Format("{0}widgets/{1}/widget.ascx", Utils.RelativeWebRoot, widget.InnerText);
                try
                {
                    var control = (WidgetBase)this.Page.LoadControl(fileName);
                    if (widget.Attributes != null)
                    {
                        control.WidgetId  = new Guid(widget.Attributes["id"].InnerText);
                        control.Title     = widget.Attributes["title"].InnerText;
                        control.ShowTitle = control.IsEditable
                                                ? bool.Parse(widget.Attributes["showTitle"].InnerText)
                                                : control.DisplayHeader;
                    }

                    control.ID   = control.WidgetId.ToString().Replace("-", string.Empty);
                    control.Zone = this.zoneName;

                    control.LoadWidget();

                    // This will return the WidgetContainer with the control in it.
                    var widgetContainer = WidgetContainer.GetWidgetContainer(control, widgetContainerExists, widgetContainerVirtualPath);
                    this.Controls.Add(widgetContainer);
                }
                catch (Exception ex)
                {
                    var lit = new Literal
                    {
                        Text = string.Format("<p style=\"color:red\">Widget {0} not found.<p>", widget.InnerText)
                    };
                    lit.Text += ex.Message;
                    if (widget.Attributes != null)
                    {
                        lit.Text +=
                            string.Format(
                                "<a class=\"delete\" href=\"#\" onclick=\"BlogEngine.widgetAdmin.removeWidget('{0}');return false\" title=\"{1} widget\">X</a>",
                                widget.Attributes["id"].InnerText,
                                labels.delete);
                    }

                    this.Controls.Add(lit);
                }
            }
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
        /// </summary>
        /// <param name="e">
        /// The <see cref="T:System.EventArgs"></see> object that contains the event data.
        /// </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            XmlNodeList zone = null;

            if (this.XmlDocument != null)
            {
                zone = this.XmlDocument.SelectNodes("//widget");
            }

            if (zone == null)
            {
                return;
            }

            // This is for compatibility with older themes that do not have a WidgetContainer control.
            var widgetContainerExists      = WidgetContainer.DoesThemeWidgetContainerExist(true);
            var widgetContainerVirtualPath = WidgetContainer.GetThemeWidgetContainerVirtualPath(false);

            foreach (XmlNode widget in zone)
            {
                var fileName = string.Format("{0}Custom/Widgets/{1}/widget.ascx", Utils.ApplicationRelativeWebRoot, widget.InnerText);
                try
                {
                    //Mod RonC Added this conditional so that the admin widget isn't loaded for non authenticated
                    //         visitors.  This shaves 1 sec off of a cold server first page fetch
                    //         for a non admin visitor.
                    bool isAdminWidget = (fileName.ToLower().IndexOf("/administration/widget.ascx") >= 0);
                    if (!isAdminWidget || (isAdminWidget && Security.IsAuthenticated))
                    {
                        var control = (WidgetBase)this.Page.LoadControl(fileName);
                        if (widget.Attributes != null)
                        {
                            control.WidgetId  = new Guid(widget.Attributes["id"].InnerText);
                            control.Title     = widget.Attributes["title"].InnerText;
                            control.ShowTitle = control.IsEditable
                                                                                                        ? bool.Parse(widget.Attributes["showTitle"].InnerText)
                                                                                                        : control.DisplayHeader;
                        }

                        control.ID   = control.WidgetId.ToString().Replace("-", string.Empty);
                        control.Zone = this.zoneName;

                        control.LoadWidget();

                        // This will return the WidgetContainer with the control in it.
                        var widgetContainer = WidgetContainer.GetWidgetContainer(control, widgetContainerExists, widgetContainerVirtualPath);
                        this.Controls.Add(widgetContainer);
                    }
                }
                catch (Exception ex)
                {
                    var lit = new Literal
                    {
                        Text = string.Format("<p style=\"color:red\">Widget {0} not found.<p>", widget.InnerText)
                    };
                    lit.Text += ex.Message;
                    if (widget.Attributes != null)
                    {
                        lit.Text +=
                            string.Format(
                                "<a class=\"delete\" href=\"#\" onclick=\"BlogEngine.widgetAdmin.removeWidget('{0}');return false\" title=\"{1} widget\">X</a>",
                                widget.Attributes["id"].InnerText,
                                labels.delete);
                    }

                    this.Controls.Add(lit);
                }
            }
        }