Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy simple solution to this Pin
Rattlehunter
9:29 10 Nov '09  
Generalperfect Pin
ev cabrera
7:35 5 Jun '08  
GeneralHelp... Pin
AD34
11:41 28 Apr '08  
GeneralI dont get this... Pin
Member 3851879
21:34 25 Mar '08  
GeneralA more simple way Pin
justin.moses
10:38 21 Aug '07  
GeneralRe: A more simple way Pin
psychrometrics
4:36 28 Sep '07  
QuestionRe: A more simple way Pin
justin.moses
5:11 28 Sep '07  
AnswerRe: A more simple way Pin
psychrometrics
8:26 1 Oct '07  
GeneralRe: A more simple way Pin
mln6937
23:38 15 Feb '08  
GeneralGood job Pin
CoolBreeze812
17:01 7 Aug '07  
GeneralWhat about Master page body attributes ??? Pin
CWinKY
6:20 7 Aug '07  
Questionuser control access Pin
Kamagurka
19:12 6 Aug '07  
GeneralGreat, but question Pin
Jamie Nordmeyer
6:25 30 Jul '07  
QuestionHELP I had this problem few days ago and abandoned this solution.... Pin
KarmasAgent
21:19 17 Jul '07  
Questionaccess from Control Pin
OmidH
19:58 17 Jul '07  
Generalgood!! Pin
nikhilmittal
1:49 16 Jul '07  
Generalgood job! Pin
york zhang
0:29 15 Jul '07  
GeneralGood thinking Pin
Adam Tibi
6:32 6 Jul '07  
GeneralWhy would you want to do this? Pin
Kevnz
18:53 1 Jul '07  
GeneralRe: Why would you want to do this? Pin
Chuck Bevitt
14:03 2 Jul '07  
GeneralRe: Why would you want to do this? Pin
Davide Icardi
15:08 2 Jul '07  
GeneralRe: Why would you want to do this? Pin
shroomy
8:05 16 Oct '07  
GeneralRe: Why would you want to do this? Pin
KellyLeahy
15:37 2 Jul '07  
GeneralRe: Why would you want to do this? Pin
Edelman
3:21 7 Aug '07  
GeneralGreat Pin
Ben Daniel
16:12 28 Jun '07  


Last Updated 30 Jul 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009