+1-888-365-2779
Try Now
More in this section

Forums / Designing & Styling / Sitefinity add class to navigation control template

Sitefinity add class to navigation control template

2 posts, 0 answered
  1. Bill
    Bill avatar
    2 posts
    Registered:
    15 Feb 2012
    24 Jul 2012
    Link to this post
    I have a control template in my navigation widget. I am trying to display a border under the current page. In the control template I get the current node in the control template code behind and add the class to display the border.


    hyperLink.Attributes.Add("class", "activePrimary");


    The hyperlink is rendered with 2 classes.


    <a id="ctl00_NavPrimaryTopPlaceholder_C008_ctl00_ctl00_MenuRadListView_ctrl0_MenuItemHyperLink" class="top" class="activePrimary" href="home">Home</a>


    I assume Sitefinity is placing the class="top". How can I append my class after the top?


    Thanks in advance.
  2. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    13 Aug 2012
    Link to this post
    Bill,

    Dealing with multiple classes can be a pain. I found some code, a while back, that has been useful.
    public static class WebHelper
    {
        public static void AddCssClass(this WebControl control, string cssClass)
        {
            List<string> classes;
            if (!string.IsNullOrWhiteSpace(control.CssClass))
            {
                classes = control.CssClass.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                if (!classes.Contains(cssClass))
                    classes.Add(cssClass);
            }
            else
            {
                classes = new List<string> { cssClass };
            }
            control.CssClass = string.Join(" ", classes.ToArray());
        }
     
        public static void RemoveCssClass(this WebControl control, string cssClass)
        {
            List<string> classes = new List<string>();
            if (!string.IsNullOrWhiteSpace(control.CssClass))
            {
                classes = control.CssClass.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            classes.Remove(cssClass);
            control.CssClass = string.Join(" ", classes.ToArray());
        }
    }

2 posts, 0 answered