0

So just like the title, how to modify the CSS class via code-behind? For example the following code

                    <li class="active"><a href="pages-messages.html"><span class="fa fa-comments"></span> Messages</a></li>
                    <li><a href="pages-calendar.html"><span class="fa fa-calendar"></span> Calendar</a></li>
                    <li><a href="pages-tasks.html"><span class="fa fa-edit"></span> Tasks</a></li>
                    <li><a href="pages-content-table.html"><span class="fa fa-columns"></span> Content Table</a></li>
                    <li><a href="pages-faq.html"><span class="fa fa-question-circle"></span> FAQ</a></li>
                    <li><a href="pages-search.html"><span class="fa fa-search"></span> Search</a></li>

The code above is navigation for my HTML page and the first line has li class="active", how to change it into just li and the second row which is pages-calender change it into li class="active" ?

Thanks

1
  • are you using web forms or MVC? Commented Jul 9, 2017 at 16:37

1 Answer 1

5

If you are using WebForms, then add runat="server" attributes and set unique id attribute for each li tag you need to modify in code-behind. Addition of this tags is needed to enable programmatic access from code-behind to the HTML element.

For example:

<li runat="server" id="PagesMessages" class="active"><a href="pages-messages.html"><span class="fa fa-comments"></span> Messages</a></li>
<li runat="server" id="PagesCalender"><a href="pages-calendar.html"><span class="fa fa-calendar"></span> Calendar</a></li>

Then in code-behind:

PagesMessages.Attributes["class"] = string.Empty;
PagesCalender.Attributes["class"] = "active";
Sign up to request clarification or add additional context in comments.

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.