Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Experts,

I have a master page with body tag looks like this:
ASP.NET
<body  runat="server" id="pageBody">


I need to change the body background image according to user choice from dropdownlist.
So I called master page body tag in content page code behind (dropdownlist selectedIndexChanged) like the following:
C#
HtmlGenericControl body = HtmlGenericControl)Master.FindControl("pageBody");

and then i did the following to change the background attribute:
C#
body.Attributes["style"] = "background: url('img/ic-vehicles/" + year_reg.SelectedItem.Text + "-" + make_reg.SelectedItem.Text + "-" + model_reg.SelectedItem.Text + "-" + trim + ".jpg') center center no-repeat ;background-attachment: fixed;";</pre>

The background is not changing!
I made sure the path is correct using this code:
C#
bool isexist = File.Exists(Server.MapPath("img/ic-vehicles/" + year_reg.SelectedItem.Text + "-" + make_reg.SelectedItem.Text + "-" + model_reg.SelectedItem.Text + "-" + trim + ".jpg"));


EDIT: forgot to say when I put break point i see this exception:
C#
base {System.Web.UI.HtmlControls.HtmlContainerControl} = {InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(((System.Web.UI.HtmlControls.HtmlGenericControl)(body)))).InnerText' threw an exception of type 'System.Web.HttpException'}

Any suggestions?

Thank you
Posted
Updated 1-May-15 6:45am
v2
Comments
ZurdoDev 1-May-15 13:01pm    
HtmlGenericControl has a Style property. I've had to use that before when Attributes did not work.
Samira Radwan 1-May-15 13:58pm    
still not showing using Style. thanks
ZurdoDev 1-May-15 14:00pm    
View source in the browser and see what it's being set to. Url is probably wrong.

1 solution

There is no such concept as "call a tag". With JavaScript, you can change the body background by changing its style at any time. For example:
JavaScript
document.body.style.background = "#faa85b url('background.png')";


—SA
 
Share this answer
 
Comments
Samira Radwan 1-May-15 13:56pm    
thanks for explaining (call a tag) :)
what i meant is to change the body background image according to dropdownlist selection.
I need to do it using c# code not javaScript.
Thank you.
Sergey Alexandrovich Kryukov 1-May-15 14:10pm    
C#?! Why? Why would you need an additional totally superfluous postback. This is easy with C#, but do it with JavaScript. Take whatever style you want based on select value.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900