jxycms
2/7/2017 - 6:30 AM

Add Class To Body Tag webpart

Add Class To Body Tag webpart

***ascx file
<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_CS_Other_BodyCSSClassOverride" CodeFile="~/CMSWebParts/CS/Other/BodyCSSClassOverride.ascx.cs" %>

***ascx.cs file
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CMS.Helpers;
using CMS.PortalControls;
using CMS.PortalEngine;
using CMS.UIControls;
using CMS.DocumentEngine;

/// Copyright (c) 2015 Create Studios. All Rights Reserved.
/// 
/// This SOURCE CODE FILE, which has been provided by Create Studios
/// is for use ONLY by licensed development partners or users, it includes 
/// CONFIDENTIAL and PROPRIETARY information of Create Studios.  
///
/// Maintenance History
/// 
/// Date       |  Author            |  Version  |  Description 
///-----------------------------------------------------------------------
/// 24/03/2015 |  Stuart Shepherd   |  1.0      |  Initial Code
///-----------------------------------------------------------------------
public partial class CMSWebParts_CS_Other_BodyCSSClassOverride : CMSAbstractWebPart
{
    #region "Properties"

    /// <summary>
    /// Body Id.
    /// </summary>
    public string BodyId
    {
        get
        {
            return ValidationHelper.GetString(GetValue("BodyId"), "");
        }
        set
        {
            SetValue("BodyId", value);
        }
    }

    /// <summary>
    /// Body CSS Class.
    /// </summary>
    public string BodyCSSClass
    {
        get
        {
            return ValidationHelper.GetString(GetValue("BodyCSSClass"), "");
        }
        set
        {
            SetValue("BodyCSSClass", value);
        }
    }

    #endregion
    
    #region "Methods"

    /// <summary>
    /// OnInit override
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
        if (IsVisible)
        { 
            if (!string.IsNullOrEmpty(BodyId))
            {
                
                PortalPage portalPage = this.Page as PortalPage;
                if (portalPage != null)
                {
                    portalPage.BodyParameters += " id='" + BodyId.ToString() + "'";
                }
            }

            if (!string.IsNullOrEmpty(BodyCSSClass))
            {
                CMS.DocumentEngine.DocumentContext.CurrentBodyClass += string.Format(" {0}", BodyCSSClass.ToString());
            }
        }

        base.OnInit(e);
    }

    #endregion
}