Etienne
Etienne

Reputation: 7201

Add class to ASP.NET MasterPage control

This is the code in my MasterPage:

<li id="liABOUT" runat="server"><a href="About.aspx">ABOUT</a></li>

When I am on another page referencing the MasterPage I want to add a class to this li control, something like this. Cant get it to work. Using ASP.NET 4.5

Me.Master.FindControl("ContentPlaceHolderMaster").FindControl("LiAbout").Attributes.Add("class", "active")

VB.NET or C# Code would be fine

Upvotes: 1

Views: 944

Answers (3)

Etienne
Etienne

Reputation: 7201

This worked....

' Get reference to control located on master page
 Dim lb As HtmlGenericControl = Page.Master.FindControl("liABOUT")
 lb.Attributes.Add("class", "active")

Upvotes: 0

Salah Akbari
Salah Akbari

Reputation: 39956

You can create a public property in your MasterPage :

public String LiAboutClass
{
    get { return liABOUT.Attributes["class"]; }
    set { liABOUT.Attributes["class"] = value; }
}

Access this property in your ContentPage:

var siteMaster = (SiteMaster)this.Master;
if (siteMaster != null) siteMaster.LiAboutClass = "active";

Edit: Also you can use MasterType. it allows you to access the MasterTypes properties directly.

Upvotes: 1

user5276697
user5276697

Reputation:

This works for me. I first converted it to HtmlGenericControl and then added attribute.

(Master.FindControl("liABOUT") as HtmlGenericControl).Attributes.Add("class", "active");

Upvotes: 0

Related Questions