Solved

Add BackgroundImage for DIV from .net Backend

Posted on 2012-03-15
4
402 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:itinfo7
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 37728095
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.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37728105
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.
0
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 500 total points
ID: 37728140
Declare the DIV as server side control:

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


Now in the code behind file use below code:

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

Expert Comment

by:Ajay Sharma
ID: 37728144
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now