Click here to Skip to main content
Licence 
First Posted 28 Jun 2007
Views 97,806
Bookmarked 61 times

Adding attributes to the <body> tag when using Master Pages

By Chuck Bevitt | 30 Jul 2007
Accessing the Master Page <body> tag from an ASP.NET Content page
2 votes, 7.4%
1
1 vote, 3.7%
2
3 votes, 11.1%
3
1 vote, 3.7%
4
20 votes, 74.1%
5
4.64/5 - 27 votes
3 removed
μ 4.27, σa 2.22 [?]

Problem

If you are using Master Pages in an ASP.NET application and you need to add an attribute to the <BODY> tag from a Content Page -- for instance, to set a client script function for the onload event of the page -- you will find that you can't do it directly because the <BODY> tag is in the Master Page, not in your Content Page.

Solution

Make the <BODY> tag on the Master Page a public property, so you can access it from any Content Page. First, promote the <BODY> tag in the Master Page to an ASP.NET server control. Change:

<BODY>

to:

<BODY id="MasterPageBodyTag" runat="server">

Now that the body tag is a server control, you can configure access to it as a public property in the Master Page code behind file:

using System.Web.UI.HtmlControls;
public partial class MyMasterPage : System.Web.UI.MasterPage
{
    public HtmlGenericControl BodyTag
    {
        get
        {
            return MasterPageBodyTag;
        }
        set
        {
            MasterPageBodyTag = value;
        }
    }
...

Note that the MasterPageBodyTag server control is of type System.Web.UI.HtmlControls.HtmlGenericControl. To demonstrate this, just set a breakpoint in the Page_Load function in the code behind file, run the ASP.NET project in debug mode to that point, and execute ?MasterPageBodyTag.GetType().ToString() in the Immediate Window. To use this property from a Content Page, first declare the type of your Master Page in your Content Page's ASPX file:

<%@ MasterType TypeName="MyMasterPage" %>

Then somewhere in your Content Page's code behind file, use the Master Page's BodyTag property to add an attribute to the <BODY> tag:

protected void Page_Load(object sender, EventArgs e)
{
    Master.BodyTag.Attributes.Add("onload", "SayHello()");
...

This example, of course, assumes that there is a SayHello() client script in this Content Page. Running the application to the Content Page and then viewing the source code in the browser will show that the onload="SayHello()" attribute was added to the <BODY> tag. This technique should work for any HTML tag in the Master Page that you wish to access from a Content Page.

History

  • 28 June, 2007 -- Original version posted
  • 30 June, 2007 -- Article moved to main CodeProject.com article base
  • 30 July, 2007 -- Article edited

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chuck Bevitt



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTip: If you get error "Could not load type 'MyType' Pinmembernaughton6:43 21 Mar '11  
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pinmemberhamedfilm0:09 11 Feb '12  
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pinmemberhamedfilm0:10 11 Feb '12  
GeneralMy simple solution to this PinmemberRattlehunter9:29 10 Nov '09  
Generalperfect Pinmemberev cabrera7:35 5 Jun '08  
GeneralHelp... PinmemberAD3411:41 28 Apr '08  
GeneralI dont get this... PinmemberMember 385187921:34 25 Mar '08  
GeneralA more simple way Pinmemberjustin.moses10:38 21 Aug '07  
If you set your master page body tag to:

<body id="master_body" runat="server">

 
Then all you need to do is add this on any page that uses the master:

public void Page_Load(Object sender, EventArgs e)
{
//Inject onload and unload
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("master_body");
body.Attributes.Add("onload", "someFunction()");
body.Attributes.Add("onunload", "anotherFunction()");

}

 
I don't use code behind pages and this works great.
GeneralRe: A more simple way Pinmemberpsychrometrics4:36 28 Sep '07  
QuestionRe: A more simple way Pinmemberjustin.moses5:11 28 Sep '07  
AnswerRe: A more simple way Pinmemberpsychrometrics8:26 1 Oct '07  
GeneralRe: A more simple way Pinmembermln693723:38 15 Feb '08  
GeneralGood job PinmemberCoolBreeze81217:01 7 Aug '07  
QuestionWhat about Master page body attributes ??? PinmemberCWinKY6:20 7 Aug '07  
Questionuser control access PinmemberKamagurka19:12 6 Aug '07  
GeneralGreat, but question PinmemberJamie Nordmeyer6:25 30 Jul '07  
QuestionHELP I had this problem few days ago and abandoned this solution.... PinmemberKarmasAgent21:19 17 Jul '07  
Questionaccess from Control PinmemberOmidH19:58 17 Jul '07  
Generalgood!! Pinmembernikhilmittal1:49 16 Jul '07  
Generalgood job! Pinmemberyork zhang0:29 15 Jul '07  
GeneralGood thinking PinmemberAdam Tibi6:32 6 Jul '07  
QuestionWhy would you want to do this? PinmemberKevnz18:53 1 Jul '07  
AnswerRe: Why would you want to do this? PinmemberChuck Bevitt14:03 2 Jul '07  
GeneralRe: Why would you want to do this? PinmemberDavide Icardi15:08 2 Jul '07  
GeneralRe: Why would you want to do this? Pinmembershroomy8:05 16 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 30 Jul 2007
Article Copyright 2007 by Chuck Bevitt
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid