0

My asp.net site has a navigation that uses an .active css tag to change the color of the navigation color on active pages.

This is in my site.master header

.active
{
 background-color: #c62020; 
 border-right: 1px solid;
 margin-left: -1px;
}

</style>

This works great as is. But now I want it to change the background color depending on a variable, I have the code to do it in the Site.Master.cs.

I understand that I cant use <%= %> inside the header. Whats the best way to get this to work?

Thanks

3
  • you can add server tags to the head, just be careful how, as they render differently. see stackoverflow.com/questions/8104268/…
    – ericosg
    Apr 9, 2013 at 18:21
  • Thanks for the suggestion. I tried that and got 'WebApp.SiteMaster.hextag' is inaccessible due to its protection level. Even though I put string hextag = "#c62020" in the public class SiteMaster : System.Web.UI.MasterPage. I will keep playing with it and see why I get that error.
    – Zach
    Apr 9, 2013 at 18:30
  • ViewData/ViewBag or a custom rendering section perhaps?
    – Daniel
    Apr 9, 2013 at 18:32

2 Answers 2

0

Why not putting your css class outside of head and put it in the body and then you can use server tags there <% %>

The another way is through jQuery or javascript

$(document).ready(function(){
      $('.active').css("background-color","<%=serverVariable%>");
});
2
  • In the Site.Master.CS file how do I set the serverVariable? I must be doing something wrong, I jsut want my serverVariable = "#c62020".
    – Zach
    Apr 9, 2013 at 18:47
  • I got it working forgot to make the servervariable public. If I put the css in the first link of the <body> it doesn't render.
    – Zach
    Apr 9, 2013 at 19:39
0

I don`t know this is best way or not. I am also beginner. But I have done this as follow.. call JavaScript function from server side in your .cs file like

    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "keyforfun",fun_setHeadcolor(your_variable), true);

Then in your site.master page add javaScript function called "fun_setHeadcolor" like

function fun_setHeadcolor(var1)
{ 
  //do stuff here to change background color
  //for example   $('.active').css("background-color",var1);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.