Link to home
Start Free TrialLog in
Avatar of itinfo7
itinfo7

asked on

Add BackgroundImage for DIV from .net Backend

Hello Experts,

I have Div in Front end like this.

<div id="frontDiv" style="float:left;">


I want to give background image from .net backend Like this

DivFront.Attributes.Add("style", "background-Image:url(http://localhost/Templates/Products/POD/VCA/dog.JPG)");

but it's not taking background image for that DIV.I really don't know is there any other method for giving background-image for DIV.

Please guide me in this.
Avatar of Mrunal
Mrunal
Flag of India image

Hi,
Is there any specific reason for assigning background color to div from code behind?

Better approach is using CSS.

Here is exmple:

<html>
<head>
<style type="text/css">
.testDiv {background-image:url('paper.gif');}
</style>
</head>

<body>
<h1>Hello World!</h1>
<div class='testDiv' style='height:200px;'>test data in div
</div>
</body>
</html>

Open in new window


Is still you want to use it from code behind then you have to make that div as server side control so that you can access it from code behind or simply you can take Panel control and use it's "BackImageUrl" property to assign background image.
Add runat="server" and then you will be able to access it in your code behind file:-

<div id="frontDiv" style="float:left;" runat="server">

Next check that this url does show up the image and not 404 error.
ASKER CERTIFIED SOLUTION
Avatar of Ajay Sharma
Ajay Sharma
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, the code behind needs to be written as below:

frontDiv.Attributes.Add("style", @"background-Image:url(""" + Server.MapPath("~/Templates/Products/POD/VCA/dog.JPG") + @""");

Here the Templates directory should be in your root.