2

I have an ASP.NET content page. The HTML tag associated with the content page is declared in the master page. I need to add an attribute to the HTML tag from the content page. My problem is, I do not know how to access this HTML tag from the content page.

Can someone please tell me how to do this?

Thank you

3 Answers 3

1

I wrote a routine this morning that helps me get a handle on master page controls recursively up the master page hierarchy (if you're using nested master pages). I implemented it as an extension method. Perhaps you'll find it useful:

public static TResult FindControlInMasterPages<TResult>(this MasterPage masterPage, string id)
    where TResult : Control
{
    return masterPage == null ? null :
           masterPage.FindControl(id) as TResult ?? FindControlInMasterPages<TResult>(masterPage.Master, id);
}

You could apply that like so:

this.Master.FindControlInMasterPages<Label>("myLabel").Attributes["attr"] = "value";
Sign up to request clarification or add additional context in comments.

Comments

1

You can always expose it as a property on your Master Page if you have to

public Label MyLabel
{
    get { return myLabel; }
}

You can then easily access it from your content page:

var master = Master as MyAwesomeMasterPage;

if(master != null)
    ((MyAwesomeMasterPage)Master).MyLabel.Attributes["bgcolor"] = "lightblue";

This MSDN article will give you more detail, including how to get your Master page property to be strongly typed.

Comments

0

I did this once with an ugly hack, as with all hacks it was due to a lack of time to figure out how to do it properly.

I replaced the <HTML> tag in the master page with a <asp:Literal /> control and then exposed that literal control as a property of the master page which I could access from the content page.

It's a horrible way to do it, I'm sure there will be much better answers than this but if you're in a hole it might help.

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.