Skip to content Skip to sidebar Skip to footer

Assigning Css Class To A Masterpage Control From Contentpage In Asp.net

I have a unordered list in my master page....

Solution 1:

Solution 2:

I once had a similar issue though I think it is much more simple than what both of you are proposing here. To apply a css object to a server contol from a control to a Master Page, you drop this into each of the pages

in the control page add

MasterPageFile="~/MyMasterPage.master"

in the code behind files of your controls

using System.WEB.UI.Htmlcontrols;//add your namespace//

HtmlGenericControls mycontrol = (HtmlGenericControl)this.Page.Master.FindControl("yourcontrolname") as HtmlGenericControl;

mycontrol.Attributes.Add("class", "cssToApply");

It does not store the current pages added class it is destroyed once you leave the page and visit another so you dont need to worry about creating repetition

<div class"X"class"X"class"X">

In this scenario "mycontrol" applies to a navigation menu of list items where I wanted the current pages navigation item to highlight while on the page. This can also apply not only to CSS class but also CSS id. This technique can be applied without the use of importing or making any major changes to inherit the master page file.

I hope this helps and I now have a resource out there to help me when I forget how to do this haha.

Post a Comment for "Assigning Css Class To A Masterpage Control From Contentpage In Asp.net"