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.
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:
You could even remove the dependency on the id by searching for an
HtmlGenericcontrol with a tag name of “body”: