2

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.

4
  • Take a look at this thread: stackoverflow.com/questions/2530784/… Commented Apr 26, 2012 at 6:36
  • Yes, it's not difficult to do. What have you tried and where are you getting stuck? Commented Apr 26, 2012 at 6:36
  • Well pull out the images from storage and present it to him, when he selects one, point up the background to that resource, Where have you got stuck ? Commented Apr 26, 2012 at 6:36
  • Sorry this is my first time using asp.net, I just want it to change the image the background is pointed at, change the path kind of thing. Commented Apr 26, 2012 at 6:38

2 Answers 2

2

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");

Sign up to request clarification or add additional context in comments.

Comments

1

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....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.