tags:

views:

30

answers:

2
<div class="sp1" style="background-image:url(<%#GetImage()%>);" runat="server">&nbsp;</div>

Tested my method by assigning the String(containing my image's path) returned by it to a label..its getting the path alright..then why wont it display when I run the code?

when I viewed the page's source..this is what I see..

 <div class="sp1" style="background-image:url(&lt;%#GetImage()%>);">&nbsp;</div>
+1  A: 

Remove the runar="server", you do not need that on this div.

Then its not going to change the <%, also if the GetImage is return string, you just need to type <%=GetImage()%>

Other way is to use a literal and make a full render of the div on the code behind.

Aristos
I gave the full path of the image in background-image property..I can see the path in the Page source but image's still not showing..what could be wrong?
Serenity
What is the full path of the image?
Tim B James
@Happy maybe you div did not have the size to show the image ? Inspect it with browser tools to check it out.
Aristos
+2  A: 

DataBinding syntax <%# %#> only works if you are calling DataBind on the control or you are inside a databound control. Secondly the databinding syntax cannot set part of a property, you need to include the entire content of the property you want to bind (I believe this is true).

For your div if you have it inside a repeater or you want to call DataBind() on the control serverside try changing the style atribute to

style ='<%# string.Format("background-image:url({0});", GetImage()) %>'

Otherwise if it is not inside a databound control then remove the runat="server" and use <%=GetImage() %> to just output the image path when the page is rendered.

<div class="sp1" style="background-image:url(<%=GetImage()%>);">&nbsp;</div>
Chris Mullins