Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add BackgroundImage for DIV from .net Backend

Posted on 2012-03-15
4
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

730 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