Click here to Skip to main content
15,348,965 members
Articles / Web Development / ASP.NET
Posted 28 Jun 2007


63 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.66/5 (30 votes)
30 Jul 20072 min read
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 Pin
vadmin7-Apr-15 17:31
Membervadmin7-Apr-15 17:31 
QuestionMaster page body tag in Visual Studio 2010 Pin
Member 800638431-Dec-13 7:44
MemberMember 800638431-Dec-13 7:44 
GeneralTip: If you get error "Could not load type 'MyType' Pin
naughton21-Mar-11 5:43
Membernaughton21-Mar-11 5:43 
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pin
hamedfilm10-Feb-12 23:09
Memberhamedfilm10-Feb-12 23:09 
GeneralRe: Tip: If you get error "Could not load type 'MyType' Pin
hamedfilm10-Feb-12 23:10
Memberhamedfilm10-Feb-12 23:10 
GeneralMy simple solution to this Pin
Rattlehunter10-Nov-09 8:29
MemberRattlehunter10-Nov-09 8:29 
Generalperfect Pin
ev cabrera5-Jun-08 6:35
Memberev cabrera5-Jun-08 6:35 
GeneralHelp... Pin
AD3428-Apr-08 10:41
MemberAD3428-Apr-08 10:41 
GeneralI dont get this... Pin
Member 385187925-Mar-08 20:34
MemberMember 385187925-Mar-08 20:34 
GeneralA more simple way Pin
Justincc21-Aug-07 9:38
professionalJustincc21-Aug-07 9:38 
GeneralRe: A more simple way Pin
psychrometrics28-Sep-07 3:36
Memberpsychrometrics28-Sep-07 3:36 
QuestionRe: A more simple way Pin
Justincc28-Sep-07 4:11
professionalJustincc28-Sep-07 4:11 
AnswerRe: A more simple way Pin
psychrometrics1-Oct-07 7:26
Memberpsychrometrics1-Oct-07 7:26 
GeneralRe: A more simple way Pin
mln693715-Feb-08 22:38
Membermln693715-Feb-08 22:38 
GeneralGood job Pin
CoolBreeze8127-Aug-07 16:01
MemberCoolBreeze8127-Aug-07 16:01 
QuestionWhat about Master page body attributes ??? Pin
CWinKY7-Aug-07 5:20
MemberCWinKY7-Aug-07 5:20 
Questionuser control access Pin
Kamagurka6-Aug-07 18:12
MemberKamagurka6-Aug-07 18:12 
GeneralGreat, but question Pin
Jamie Nordmeyer30-Jul-07 5:25
MemberJamie Nordmeyer30-Jul-07 5:25 
QuestionHELP I had this problem few days ago and abandoned this solution.... Pin
mindpivot17-Jul-07 20:19
Membermindpivot17-Jul-07 20:19 
Questionaccess from Control Pin
OmidH17-Jul-07 18:58
MemberOmidH17-Jul-07 18:58 
Generalgood!! Pin
nikhilmittal16-Jul-07 0:49
Membernikhilmittal16-Jul-07 0:49 
Generalgood job! Pin
york zhang14-Jul-07 23:29
Memberyork zhang14-Jul-07 23:29 
GeneralGood thinking Pin
Adam Tibi6-Jul-07 5:32
professionalAdam Tibi6-Jul-07 5:32 
QuestionWhy would you want to do this? Pin
Kevnz1-Jul-07 17:53
MemberKevnz1-Jul-07 17:53 
AnswerRe: Why would you want to do this? Pin
Chuck Bevitt2-Jul-07 13:03
MemberChuck Bevitt2-Jul-07 13:03 

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

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