Changing background image in ASP.net with c#
Solution 1
change your body tag as below
body id="bdy1" runat="server"
now change your .aspx.cs page
write the following on page load
1) If you want image background then
bdy1.Attributes.Add("style", "background:url(images/tulips.jpg);");
2) if you want color as background then
bdy1.Attributes.Add("style", "background:teal");
Solution 2
for changing background dynamically you need to do as below.
place dropdown and button in aspx page as below.
<input type="button" value="Change BG" onclick="ChangeBG();" />
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem>bg_1.jpg</asp:ListItem>
<asp:ListItem>bg_2.jpg</asp:ListItem>
<asp:ListItem>bg_3.jpg</asp:ListItem>
<asp:ListItem>bg_4.jpg</asp:ListItem>
</asp:DropDownList>
define javascript function as below in head section
<script type="text/javascript" language="javascript" >
function ChangeBG() {
var ddl = document.getElementById("DropDownList1");
var strimg = ddl.options[ddl.selectedIndex].value;
document.body.background = strimg;
}
</script>
then most important call ChangeBG() function on load event of body.
<body onload="ChangeBG();" >
you can also set background on dropdown change event.
Hope this will helps you..happy coding....
John Peters
Updated on June 04, 2022Comments
-
John Peters almost 2 years
Im currently trying to design a webpage, I have set the background image to a image in my website folder. The asp.net code shows up as:
body background="ProtectedPages/Storage/green.png"
I was wondering if there was a way to change this with a button, using c# code. I am trying to make it so the user of the website can change the background image from a list of options, any help would be appreciated:)
Cheers, John.