Site Search:
Sign in | Join | Help
4Penny.net

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding

ASP.NET 2.0 background-images won't display properly

Since the introduction of ASP.NET 2.0, I have struggled with images and background images. The issue is getting the reference in the master page to correctly find images when you navigate to pages in subdirectories. The image tag that worked in the root directory will no longer work in the subdirectory.

My first solution for this was to switch to asp:image tags, and use "~/images/myImage.jpg". That solved most of the issues.

It doesn't solve the issue for background images. As hard as I tried, I could not make anything work. Finally, I stumbled accross this technique:

In the HTML, form the body tag with a runat="server", like this:
body runat="server" id="myBody

Then in the Page_Load, use this code:

'dynamically assign a background image to an HTML element
Dim strStyle As String
'build the style
strStyle = "background-repeat:repeat;background-image: url(" & resolveUrl("~\images\bg01.gif") & ")"
'assign it
Me.myBody.Attributes("style") = strStyle
Published Jan 11 2007, 08:14 PM by Steve Gray
Filed under:

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.