469,944 Members | 2,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,944 developers. It's quick & easy.

Dynamically switching a background image

I am creating a website using .Net v2.0 and c# and I have a problem with background images.

I want to dynamically change the background image on the page, depending on data in the SQL database. If I place an <asp> label after the <body> tag, within one of the .NET containers, then it is easy to get the c# code to dynamically change the background image by placing suitable HTML on the label. The problem is that the background image does not replicate itself down through the whole screen, but stops at the end of the HTML content. In contrast, the background colour, which is set within the <body> tag does cover the whole screen as required. Therefore I concluded that the background image needs to be placed within the <body> tag. Unfortunately, the Visual Web Developer will not allow me to place an <asp> label within the <body> tag itself. I also tried placing an <asp> label in the header, with the idea that I could dynamically switch the CSS file, but that is not allowed either. The only way I can think of is to add a series on <br /> tags at the end of the file to stretch the content down to fill the screen, but this causes other problems, such as unwanted scrolling. Has anyone got any ideas, other than using php which is much more flexible?

Many thanks

Sep 20 '07 #1
6 3205
7,872 Expert 4TB
Why not set the background of the page and not of a label?
Sep 20 '07 #2
Sounds good, but how do you do it? (I found another similar question, and the answer was to set the body attributes, using an ID in the body tag, but, when using masterpages, the <body> is on a different page.)


Sep 24 '07 #3
7,872 Expert 4TB
Can a masterpage have a css file linked? (or in the head section?)

Because you can set the background image in CSS
Sep 24 '07 #4
Many thanks for your interest.

I am already setting the background image via CSS. The problem is switching it to a different image under program control.
Oct 2 '07 #5
7,872 Expert 4TB
You can manipulate the STYLE property of objects in your backend code.
Try that?
Oct 2 '07 #6
Many thanks,

I have got it all to work as follows:

In the Master page add the following javascript:

<script type="text/javascript">
function set_background(ChartType)
if (ChartType == "Demo")
document.body.style.background="#FCEFC3 url('images/Demo_Watermark.gif')";
if (ChartType == "Working")
document.body.style.background="#FCEFC3 url('images/Working_Watermark.gif')";

In the HTML body, add the following:

<asp:Label ID="Draw_Chart" runat="server" />

In the C# code, add the following:

if (Choice == "Demo")
Draw_Chart.Text = "<script type=\"text/javascript\" language=\"javascript\">set_background('Demo')</script>";
if (Choice == "Working")
Draw_Chart.Text = "<script type=\"text/javascript\" language=\"javascript\">set_background('Working')</script>";
Oct 10 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Jon Yeager | last post: by
1 post views Thread by C A Upsdell | last post: by
4 posts views Thread by Chris Mahoney | last post: by
5 posts views Thread by Jerry Camel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.