Setting image using a method but image's not displaying
<div class="sp1" style="background-image:url(<%#GetImage()%>);" runat="server"> </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:开发者_StackOverflow社区url(<%#GetImage()%>);"> </div>
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()%>);"> </div>
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.
精彩评论