iamserious
iamserious

Reputation: 5465

how to assign a class to one of the <li> in master page through c# from child page?

I have a master page with a bunch of menu like:

<ul id="ulMenu" runat="server">
    <li id="A">
    <li id="B">
    <li id="C">
    ..and so on ..
</ul>

and i have a function in master page which i call from child pages, like:

Master.setCurrentMenu("A");

and in the SetCurrentMenu() of the Masterpage, since I have my < UL > set to run at server, i am changing it dynamically, like:

StringBuilder myMenu = new StringBuilder(ulMenu.InnerHtml);
//replace and add class for current page
myMenu .Replace("<li id=\"" + cp + "\"", "<li class='selected' id=\""+ cp +"\"");
ulMenu.InnerHtml = myMenu.ToString();

(where cp is the "A" in the current context)

This way I am changing the innerHTML and theoretically its supposed to work just fine. but, in my < UL > i have a few server tags in between like <% ... %> so now the asp.net tells me very cheerfully that :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

What is the best thing I can do in this scenario?

Many thanks!

ps: edited this coz my < ul > were being taken as an html instead of showing up.. hmm

Upvotes: 0

Views: 338

Answers (2)

iamserious
iamserious

Reputation: 5465

This link has the correct answer, infact, somebody can delete this question since its a repeated one, I dint realize it.

I finally got this to work via:

C# - How to change HTML elements attributes

Upvotes: 1

Irina
Irina

Reputation: 36

It's a known issue. I would suggest not using code blocks in this case (it's considered a best practice use as little code blocks as possible).

Upvotes: 1

Related Questions