开发者

Setting image using a method but image's not displaying

<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:开发者_StackOverflow社区url(&lt;%#GetImage()%>);">&nbsp;</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()%>);">&nbsp;</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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜