Solved

how to apply a css background-image in the code behind file

Posted on 2007-11-28
3
1,394 Views
Last Modified: 2012-06-21
hi there,
i've come across a problem that I cant figure out looking through the options for a <div id="test" runat="server">..

I need to dynamically set a background-image to a <div>.

i have tried the following

<div <literal></literal>>
  my content
</div>

literal1.text = "style:background-image...";

(this wouldnt be a nice way of doing even if it worked)

i've also tried

<div id="panel1" runat="server">

code behind:
        panel1.ApplyStyleSheetSkin = "background-image: url()";

not sure how to use ApplyStyleSheetSkin but that was my best attempt

I could write the entire div out using a literal control I guess, like so,

<literal></literal>

literal1.text = "<div style=background-image:url...>content</div>";

but i assume there's a nicer way of doing it?  
enlighten me!
thank,
0
Comment
Question by:jimbona27
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
cmhunty earned 300 total points
ID: 20364764
You can set the background image by setting the style attribute of the div element:
        Dim strImageUrl As String = "URL_To_Image"
        panel1.Attributes.Add("style", "background-image=url(" & strImageUrl & ")")

Open in new window

0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20364799
Alternatively, you can remove the runat in the div:

<div id="panel1" style="background-image=url(<%=strImageURL%>)" />

and then set the variable up in the code:

Public strImageUrl As String

and then set that to anything you require.
0
 

Author Comment

by:jimbona27
ID: 20364896
ok thanks great stuff
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

12 Experts available now in Live!

Get 1:1 Help Now