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.
Programming Languages-OtherWeb Languages and StandardsASP.NET

Avatar of undefined
Last Comment
Ajay Sharma

8/22/2022 - Mon
Mrunal

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

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
Ajay Sharma

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ajay Sharma

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23