0

I have a navigation menu as an ASP.NET web-forms user control (ASCX). Each menu item can become active depending on the page the control is rendered at. The active state should be denoted with an additional CSS class named "active". What item to become active is controlled from the code-behind file by setting to true one of these properties: IsMenuItem1Active or IsMenuItem2Active.

Now my question is: How, based on the set property, can I modify the CssClass property of the corresponding menu item inside the view without doing any additional logic in code-behind?

<div class="menu">
    <asp:HyperLink runat="server" ID="MenuItem1" CssClass="menu-item" NavigateUrl="~/">Menu item 1</asp:HyperLink>
    <asp:HyperLink runat="server" ID="MenuItem2" CssClass="menu-item" NavigateUrl="~/">Menu item 1</asp:HyperLink>
</div>
3
  • Just add the statement to add class in code behind its not big deal. Commented Nov 14, 2013 at 20:19
  • It is a big deal as I don't want to recompile the project if I just want to change a class name. That's the solely reason why the code-behind is separated from the view, so that design aspects could be changed without messing with the logic. Commented Nov 14, 2013 at 20:23
  • And here is the right answer: stackoverflow.com/questions/19990046/… Thank you everyone! Commented Nov 14, 2013 at 23:21

3 Answers 3

0

Keeping with the separation of design and logic, it would be better to control the CssClass value from code behind. Any design changes that you will need to make can be done through the CSS represented by the active class name you're adding.

If you absolutely must do this in the view, then you could use a bit of JavaScript:

<script>
var c = '<%= IsMenuItem1Active ? "menu-item active" : "menu-item" %>'
document.getElementById('<%= MenuItem1.ClientID %>').setAttribute('class', c);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Please try following code :

            <div class="menu">
                <asp:HyperLink runat="server" ID="MenuItem1" CssClass="menu-item" NavigateUrl="~/">Menu item 1</asp:HyperLink>
                <asp:HyperLink runat="server" ID="MenuItem2" CssClass="menu-item" NavigateUrl="~/">Menu item 1</asp:HyperLink>
            </div>
            <%-- Change css here--%>
            <script type="text/javascript">
            <%if (IsMenuItem1Active == true)
             {%>
                document.getElementById("<%=MenuItem1.ClientID%>").className += " active";
           <%}
            else if (IsMenuItem2Active == true)
            {%>
                document.getElementById("<%=MenuItem2.ClientID%>").className += " active";
           <%}%>
            </script>

Comments

0

I would suggest two ways to do it. One way is by using javascript. The second way is by using asp.net lifecycle hook functions for the specific control.

First option: According to this, you could add a css class to a specific element using this:

document.getElementById("MyElement").className += " MyClass";

This call should be made in the onLoad function which is called just before the HTML DOM is created.

Second option: According to this article describing the ASP.NET lifecycle, you could use the PreRender function in order to add the css class.

Hope I helped!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.