Setting CSS styles from code behind

2 posts, 0 answers
  1. Developer
    Developer avatar
    43 posts
    Member since:
    Apr 2014

    Posted 23 Sep 2016 Link to this post

    I am using a rad org chart.  Basically I want to apply certain CSS styles based on a condition.
    I have figured out how to just override a css styles:

    .rocToolbar_Metro .rocToolbarButton {   background-color: #32b330 !important;}

    But I can’t figure out how to do this conditionally via the code behind, so saying if condition == 1 then apply this:

    .rocToolbar_Metro .rocToolbarButton {    background-color: #32b330 !important;}

    Else apply this:

    .rocToolbar_Metro .rocToolbarButton {    background-color: #25a0da !important;}

    I am planning to do this within the NodeDataBound event.  Any suggestions appreciated.

  2. Ivan Danchev
    Ivan Danchev avatar
    1304 posts

    Posted 27 Sep 2016 Link to this post


    If you want to apply styles to the control with class ".rocToolbarButton " depending on a condition on the server you can set a custom class through the control's CssClass property. So let's say you want to set an asp:Button's background-color:
    if (your condition)
         Button1.CssClass = "MyCustomClass1";
        Button1.CssClass = "MyCustomClass2";

    you will have two rules that use these classes as selectors:
    .MyCustomClass1 {
        background-color: #32b330 !important;
    .MyCustomClass2 {
        background-color: #25a0da !important;

    Ivan Danchev
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top