Dynamically setting a MasterPage's <body> tag style

M

Mark Rae

Hi,

It's easy enough for a child page to manipulate its MasterPage's header
simply by modifying the MasterPage thus:

<header runat="server">
....
....
</header>

Then, in code, we can do stuff like Header.Title = "Welcome";

However, is it possible to do the same for the MasterPage's <body > tag?

Specifically, I'm looking for a way to get a child page to create the
following HTML on some pages but not on others:

<body style="BACKGROUND-POSITION: center bottom; BACKGROUND-ATTACHMENT:
fixed; BACKGROUND-IMAGE: url(images/skylinelogo.gif); BACKGROUND-REPEAT:
no-repeat">

I don't really want to have a second MasterPage just for this, because this
background image is the only change to the basic layout of this site - only
the contents of the child pages changes, obviously... However, I do
appreciate that that would be a very simple and quick workaround... :)

Any assistance gratefully received.

Mark
 
G

Guest

Mark,
Wouldn't you be better off setting the body Style css in a Stylesheet
instead of inline on the tag, and then just arranging to load the correct
stylexx.css dynamically into the page?
Peter
 
M

Mark Rae

Peter,
Wouldn't you be better off setting the body Style css in a Stylesheet
instead of inline on the tag, and then just arranging to load the correct
stylexx.css dynamically into the page?

Yes, I could do that.

So, is there no easy way of manipulating a MasterPage's <body /> tag in the
same way is its <head /> tag...?

Mark
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
Peter,


Yes, I could do that.

So, is there no easy way of manipulating a MasterPage's <body /> tag in the
same way is its <head /> tag...?

Mark

Aren't you missing the point here? If you set the style using a css file
you don't have to do any manipulation of the body tag at all.
 
M

Mark Rae

Aren't you missing the point here? If you set the style using a css file
you don't have to do any manipulation of the body tag at all.

Dynamically...

Sometimes the child page needs the background, sometimes it doesn't...
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
Dynamically...

Sometimes the child page needs the background, sometimes it doesn't...

Yes, dynamically. You can for instance use themes.
 
J

JT

Sometimes people just want their questions answered. Seems reasonable,
especially since I'm interested in the same answer.
 
J

John Timney \(MVP\)

The answer is probably quite sparse as people are following the methods
suggested in the thread. You should not set the body tag, you should change
the master page dynamically. If you wish to set only a CSS element (like
body styling) then switch the CSS to an alternate. You should avoid
embedding CSS against single elements as it creates less seperation between
logic and UI.

If you want to work just with the body tag then you could allocate a runat
server attribute to your body tag In the master page.
<body runat="server" id="masterBody">

in the Page_Load event of the container add something like:
HtmlGenericControl body = (HtmlGenericControl)
Page.Master.FindControl("masterBody");
body.Attributes.Add("style", "BACKGROUND-POSITION: center bottom;");

You would only need to add this to load events in containers that required
it. I've never tried it mind you, it might not work!!!

Regards

John Timney (MVP)
 
M

Mark Rae

John,
If you want to work just with the body tag then you could allocate a runat
server attribute to your body tag In the master page.
<body runat="server" id="masterBody">

in the Page_Load event of the container add something like:
HtmlGenericControl body = (HtmlGenericControl)
Page.Master.FindControl("masterBody");
body.Attributes.Add("style", "BACKGROUND-POSITION: center bottom;");

You would only need to add this to load events in containers that required
it. I've never tried it mind you, it might not work!!!

Thanks for this! Firstly, I can confirm that it does work... :)

However, curiously, the line which instantiates the HtmlGenericControl
object generates an error which is not caught by the standard try..catch
handler. By which I mean that stepping through the code below I can see that
an error is raised, but this doesn't jump into the error handler, and
continues through the code.

The MasterPage body tag is as follows:

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

The Page_Load of the child page is as follows:

protected void Page_Load(object sender, EventArgs e)
{
try
{
HtmlGenericControl objBody =
(HtmlGenericControl)Master.FindControl("objBody");
objBody.Attributes.Add("style", "background-image:
url(../images/skylinelogo.gif);");
}
catch (Exception ex)
{
CApplication.GlobalExceptionHandler(ex);
}
}

The error is:

{InnerText =
'((System.Web.UI.HtmlControls.HtmlContainerControl)(objBody)).InnerText'
threw an exception of type 'System.Web.HttpException'}
base {System.Web.UI.HtmlControls.HtmlContainerControl}: {InnerText =
'((System.Web.UI.HtmlControls.HtmlContainerControl)(((System.Web.UI.HtmlControls.HtmlGenericControl)(objBody)))).InnerText'
threw an exception of type 'System.Web.HttpException'}
TagName: "body"

Now, can anyone please tell me:

1) What the error means?

2) Why it isn't caught by the try...catch handler?

3) Is it safe to ignore it, since the code proceeds past it and does what
it's supposed to do?

Any assistance gratefully received.

Mark
 
J

John Timney \(MVP\)

To hazard a guess (I dont have a dev environment handy) could it be because
you have called your generic control and your body server control the same
names objBody, and your telling findcontrol to look for itself

Regards

John Timney (MVP)
 
M

Mark Rae

To hazard a guess (I dont have a dev environment handy) could it be
because you have called your generic control and your body server control
the same names objBody, and your telling findcontrol to look for itself

Nope - I already thought of that and changed the variable name, but that
made no difference.

Incidentally, doing the whole thing in one long line doesn't generate the
error... e.g.

This generates the error, on the first line:

HtmlGenericControl HTMLBody =
(HtmlGenericControl)Master.FindControl("objBody");
HTMLBody .Attributes.Add("style", "background-image:
url(images/skylinelogo.gif);");

But this doesn't:

((HtmlGenericControl)Master.FindControl("objBody")).Attributes.Add("style",
"background-image: url(images/skylinelogo.gif);");

Both work perfectly...

This has really piqued my curiosity now! Can anyone shed any light on what's
happening here...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top