Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:35:36+00:00 2026-05-14T21:35:36+00:00

All I want to do is access the <body> element from the code-behind of

  • 0

All I want to do is access the <body> element from the code-behind of a content page and add a class name to it.

I have a top-level master page with the <body> element in it. Then I have a nested master page which is the master page for the content page. From the code behind of the content page I want to add a class name to the body element. That’s all.

I have this in the top-level master:

<body id="bodyNode" runat="server">

I added this to the code-behind for the content page:

Master.bodyNode.Attributes.add("class", "home-page");

And I get a message that:

System.Web.UI.MasterPage’ does not contain a definition for ‘bodyNode

If I add this to the aspx content page:

<% @ MasterType VirtualPath="~/MasterPage.master"%>

The message then changes to:

bodyNode is inaccessible due to its protection level

Please advise, I’ve wasted like 2 hours on what feels like something that should be really simple to do 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:35:37+00:00Added an answer on May 14, 2026 at 9:35 pm

    once you have set runat=”server” for your body node, you have to access it using the HTMLControls namespace. try this.

    public void Page_Load(Object sender, EventArgs e)
    { 
    //Inject onload and unload
    HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("bodyNode");
    body.Attributes.Add("class", "home-page");   
    }
    

    EDIT
    Your problem is that you have nested master pages.

    Since the “body” tag is in your top level master page, Master.FindControl() won’t work, as that is looking in the nested master page.

    What you need to do is use Master.Master.FindControl(), or recursively loop through your master pages, going up until Master.Master is null (as then you know you are at the top level master page) and then calling FindControl() on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to override access to one variable in a class, but return all
Say I have an interface IFoo and I want all subclasses of IFoo to
I want to be access some variables I've assigned dynamically from PHP in Smarty,
All I want is to be able to change the color of a bullet
All I want is to update an ListViewItem's text whithout seeing any flickering. This
I want: all links which not contained filename (not .html, .jpg, .png, .css) redirect
I am reading log files but not all lines want to be processed straight
Here's a simple (hopefully) L10N question: Do all locales want this format: Sunday, Nov
I'm building the world's simplest library application. All I want to be able to
Disclaimer: I'm fairly new to python! If I want all the lines of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.