Click here to Skip to main content
6,295,667 members and growing! (11,453 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner

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

By Chuck Bevitt

Accessing the Master Page <body> tag from an ASP.NET Content page
C#, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Posted:28 Jun 2007
Updated:30 Jul 2007
Views:49,919
Bookmarked:50 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
23 votes for this article.
Popularity: 5.73 Rating: 4.21 out of 5
2 votes, 8.7%
1
1 vote, 4.3%
2
3 votes, 13.0%
3
1 vote, 4.3%
4
16 votes, 69.6%
5

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


Member

Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
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  
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  
GeneralWhat 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  
GeneralWhy would you want to do this? PinmemberKevnz18:53 1 Jul '07  
GeneralRe: 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  
GeneralRe: Why would you want to do this? PinmemberKellyLeahy15:37 2 Jul '07  
GeneralRe: Why would you want to do this? PinmemberEdelman3:21 7 Aug '07  
GeneralGreat PinmemberBen Daniel16:12 28 Jun '07  
GeneralBody tag... PinmemberKellyLeahy15:16 28 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jul 2007
Editor: Genevieve Sovereign
Copyright 2007 by Chuck Bevitt
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project