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 7605643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:13:09+00:00 2026-05-31T00:13:09+00:00

I have masterpage which has runat=server & Id set on body tag. see below

  • 0

I have masterpage which has runat=”server” & Id set on body tag. see below

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

code behind on masterpage I’ve added the following code:

    public HtmlGenericControl BodyTag
{
    get { return MasterPageBodyTag; }
    set { MasterPageBodyTag = value; }
}

now I want to add css class to body tag from Class1.cs file in App_code folder.

On the .aspx am passing the master page control using the following code:

  protected void Page_Load(object sender, EventArgs e)
{
    backend.FindPage((PageTemp)this.Master);

}

Now on Class1.cs I have the following

  public static void FindPage(Control mp)
{

    Page pg = (Page)HttpContext.Current.Handler;


    PropertyInfo inf = mp.GetType().GetProperty("BodyTag");    

}

I want to add the following to found BodyTag

 //      BodyTag.Attributes.Add("class", "NewStyle");

But can’t seem to find a way to add atrribute or cast the inf to HtmlGenericControl.

Any help would be great.

  • 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-31T00:13:11+00:00Added an answer on May 31, 2026 at 12:13 am

    Rather than having a dependency on the Master Page type, I’d simply use FindControl to search for the body element by Id. Assuming the body tag is on your top-level Master page, and also assuming you may be using nested master pages, it would look something like:

    private static MasterPage GetTopLevelMasterPage(Page page)
    {
        MasterPage result = page.Master;
        if (page.Master == null) return null;
    
        while(result.Master != null)
        {
            result = result.Master;
        }
    
        return result;
    }
    
    private static HtmlGenericControl FindBody(Page page)
    {
        MasterPage masterPage = GetTopLevelMasterPage(page);
        if (masterPage == null) return null;
        return masterPage.FindControl("MasterPageBodyTag") as HtmlGenericControl;
    }
    
    private void UpdateBodyCss()
    {
        HtmlGenericControl body = FindBody(this);
        if(body != null) body.Attributes.Add(...);
    }
    

    You could even remove the dependency on the id by searching for an HtmlGeneric control with a tag name of “body”:

    private static HtmlGenericControl FindBody(Page page)
    {
        MasterPage masterPage = GetTopLevelMasterPage(page);
        if (masterPage == null) return null;
        foreach(Control c in masterPage.Controls)
        {
            HtmlGenericControl g = c as HtmlGenericControl;
            if (g == null) continue;
            if (g.TagName.Equals("body", StringComparison.OrdinalIgnoreCase)) return g;
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a master page in which I have the following code. <head runat=server>
I have a master page which has a <form runat=server> line and a ContentPlaceHolder
I have a website on with MVC, which has a dropdown in the masterpage.
I have a master page in my asp.net MVC project, which has code like
I have an MVC masterpage set up which loads a dynamic menu using AJAX
I have a div in default.aspx which has a masterpage. Masterpage has a expand/collapse
I have a nested div (see below) which have different CSS classes to give
I have a master page called MasterPage.master which has a <fieldset> with a <legend>
I have a main MasterPage which has a single column layout for the web
I have a search textbox situated on a masterpage like so: <asp:TextBox ID=frmSearch runat=server

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.