Solved

Add BackgroundImage for DIV from .net Backend

Posted on 2012-03-15
4
405 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

770 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