Click here to Skip to main content
Click here to Skip to main content

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

, 30 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Accessing the Master Page <body> tag from an ASP.NET Content page


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.


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 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
            return MasterPageBodyTag;
            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.


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


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
No Biography provided

Comments and Discussions

SuggestionSimple alternative Pinmembervadmin7-Apr-15 18:31 
QuestionMaster page body tag in Visual Studio 2010 PinmemberMember 800638431-Dec-13 8:44 
GeneralTip: If you get error "Could not load type 'MyType' Pinmembernaughton21-Mar-11 6:43 
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pinmemberhamedfilm11-Feb-12 0:09 
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pinmemberhamedfilm11-Feb-12 0:10 
GeneralMy simple solution to this PinmemberRattlehunter10-Nov-09 9:29 
Generalperfect Pinmemberev cabrera5-Jun-08 7:35 
GeneralHelp... PinmemberAD3428-Apr-08 11:41 
GeneralI dont get this... PinmemberMember 385187925-Mar-08 21:34 
I am new to, so this might be a silly question Smile | :)

Most tutorials i have followed, has a masterpage. The contentplaceholder is a holder for the aspx pages and usercontrols
are where you can have MenuSystem, and so on in a usercontrol and link it to the masterpage.

isent it better to make a <body> in a aspx page global rather than the master's body? If you have a create Textbox button control inside a user control, in the masterpage, you would rather create to a certain aspx page than the masterpage body - or am i wrong??? Smile | :) </body>
GeneralA more simple way Pinmemberjustin.moses21-Aug-07 10:38 
GeneralRe: A more simple way Pinmemberpsychrometrics28-Sep-07 4:36 
QuestionRe: A more simple way Pinmemberjustin.moses28-Sep-07 5:11 
AnswerRe: A more simple way Pinmemberpsychrometrics1-Oct-07 8:26 
GeneralRe: A more simple way Pinmembermln693715-Feb-08 23:38 
GeneralGood job PinmemberCoolBreeze8127-Aug-07 17:01 
QuestionWhat about Master page body attributes ??? PinmemberCWinKY7-Aug-07 6:20 
Questionuser control access PinmemberKamagurka6-Aug-07 19:12 
GeneralGreat, but question PinmemberJamie Nordmeyer30-Jul-07 6:25 
QuestionHELP I had this problem few days ago and abandoned this solution.... PinmemberKarmasAgent17-Jul-07 21:19 
Questionaccess from Control PinmemberOmidH17-Jul-07 19:58 
Generalgood!! Pinmembernikhilmittal16-Jul-07 1:49 
Generalgood job! Pinmemberyork zhang15-Jul-07 0:29 
GeneralGood thinking PinmemberAdam Tibi6-Jul-07 6:32 
QuestionWhy would you want to do this? PinmemberKevnz1-Jul-07 18:53 
AnswerRe: Why would you want to do this? PinmemberChuck Bevitt2-Jul-07 14:03 
GeneralRe: Why would you want to do this? PinmemberDavide Icardi2-Jul-07 15:08 
GeneralRe: Why would you want to do this? Pinmembershroomy16-Oct-07 8:05 
AnswerRe: Why would you want to do this? PinmemberKellyLeahy2-Jul-07 15:37 
GeneralRe: Why would you want to do this? PinmemberEdelman7-Aug-07 3:21 
AnswerRe: Why would you want to do this? Pinmemberomegammx222-Jun-12 5:37 
GeneralGreat PinmemberBen Daniel28-Jun-07 16:12 
GeneralBody tag... PinmemberKellyLeahy28-Jun-07 15:16 
GeneralRe: Body tag... [modified] PinmemberChuck Bevitt2-Jul-07 13:58 
GeneralHow do you change the page's scrollbars through this? Pinmemberjemer9911-May-10 14:32 

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.

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