473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Dynamically setting a MasterPage's <body> tag style

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
Jul 10 '06 #1
12 4891
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mark Rae" wrote:
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
Jul 10 '06 #2
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:61**********************************@microsof t.com...

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
Jul 10 '06 #3
Mark Rae wrote:
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:61**********************************@microsof t.com...

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
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.
Jul 10 '06 #4
"Göran Andersson" <gu***@guffa.comwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
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...
Jul 10 '06 #5
Mark Rae wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
>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...
Yes, dynamically. You can for instance use themes.
Jul 11 '06 #6
"Göran Andersson" <gu***@guffa.comwrote in message
news:uu*************@TK2MSFTNGP05.phx.gbl...
>Dynamically...

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

Yes, dynamically. You can for instance use themes.
Or two MasterPages, even...

Thanks anyway.
Jul 11 '06 #7
example of dynamically choosing CSS as Peter suggested......

http://www.codeproject.com/aspnet/setcssfilelink.asp

Regards

John Timney (MVP)

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uy**************@TK2MSFTNGP05.phx.gbl...
"Göran Andersson" <gu***@guffa.comwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
>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...

Jul 11 '06 #8
JT

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

Jul 11 '06 #9
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)
"JT" <jt@onemain.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>
Sometimes people just want their questions answered. Seems reasonable,
especially since I'm interested in the same answer.

Jul 11 '06 #10
"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:ut******************************@eclipse.net. uk...

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
Jul 12 '06 #11
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)
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uc**************@TK2MSFTNGP03.phx.gbl...
"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:ut******************************@eclipse.net. uk...

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

Jul 12 '06 #12
"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:-O********************@eclipse.net.uk...
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...?
Jul 12 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ryan.mclean | last post by:
Hello everyone. Hope ya'll had a nice New Year. Anyway, my question is why won't this work? I must be doing something dumb . . . here is the code: in the body tag, I have this code (just to...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
5
by: Rick Spiewak | last post by:
I need to generate a "buy" button as part of an ASP.NET page - this consists of a small HTML form with hidden fields, conforming to the requirements of a merchant credit card processor. PayPal is...
2
by: James Cooke | last post by:
I want to set body attributes programmatically, from the module file: I can go into the .aspx file and say <body onload="myFunc()"> but I don't want to. Like you do with a textbox control: ...
7
by: Greg Heilers | last post by:
Perplexing problem: All of a sudden, the code I am tinkering with; renders a white-space margin around all four edges of the browser window (Mozilla, Firefox, Konqueror, and Opera). I can not...
7
by: Biguana | last post by:
OK, generally I wouldn't even try this, although it does work, but imagine, if you will, the following circumstances. I am posting content on the web, but rather than having complete control...
0
by: WB | last post by:
Hi, How can I add an "onbeforeunload" event to the body tag of the MasterPage? All pages in my site follow a MasterPage and some of these pages require long processing. I'd like to show a...
8
by: Prisoner at War | last post by:
Another weekend, another project, another problem.... Why should a perfectly fine function work only half-way through when called through onClick in an anchor tag?? The text fade-in script...
3
by: PYG | last post by:
Hi everybody I have a simple question : If i use this code : <body style="font-size:24px;color:blue;"> Text in body <table> <tr><td> Text in table
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.