Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET
Article

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 217.7K   63   34
Accessing the Master Page <body> tag from an ASP.NET Content page

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:

ASP.NET
<BODY>

to:

ASP.NET
<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:

C#
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:

ASP.NET
<%@ 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:

C#
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Why would you want to do this? Pin
Davide Icardi2-Jul-07 14:08
Davide Icardi2-Jul-07 14:08 
GeneralRe: Why would you want to do this? Pin
shroomy16-Oct-07 7:05
shroomy16-Oct-07 7:05 
AnswerRe: Why would you want to do this? Pin
KellyLeahy2-Jul-07 14:37
KellyLeahy2-Jul-07 14:37 
GeneralRe: Why would you want to do this? Pin
Edelman7-Aug-07 2:21
Edelman7-Aug-07 2:21 
AnswerRe: Why would you want to do this? Pin
omegammx222-Jun-12 4:37
omegammx222-Jun-12 4:37 
GeneralGreat Pin
Ben Daniel28-Jun-07 15:12
Ben Daniel28-Jun-07 15:12 
GeneralBody tag... Pin
KellyLeahy28-Jun-07 14:16
KellyLeahy28-Jun-07 14:16 
GeneralRe: Body tag... [modified] Pin
Chuck Bevitt2-Jul-07 12:58
Chuck Bevitt2-Jul-07 12:58 
Thanks - great response. Without thinking too deeply, one would assume that the set accessor is needed to make a change to the object. You are quite right, however, that since the get returns a reference to the object, one can simply modify the object and be done - the reference is not being assigned to a new object and the set accessor need not be enabled.Smile | :)


-- modified at 11:38 Tuesday 3rd July, 2007
GeneralHow do you change the page's scrollbars through this? Pin
jemer9911-May-10 13:32
jemer9911-May-10 13:32 

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.