I’ve got an asp:Image. I want this control to display, only if <%#Eval("Image")%> is not null.
I do not know how to write this conditional statement. What I’m trying to say is something like this (if the value of ‘image’ taken from the data structure is not null, then display the image. Otherwise, do not):
<%#Eval("Image")%> != 0 ? <asp:Image ID="image" runat="server"/>
I’m aware that this is not the syntax – consider it as pseudocode, as I have never had to write a conditional statement in the markup.
Any ideas? 🙂
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can bind the Visible property of your control to the expression and call DataBind() while the page is loading:
<asp:Image runat="server" id="image" Visible='<%#Eval("Image") != null %>' />
If you are not using server controls and want to show/hide simple markup, you can simply enclose it in an if statement:
<% if ( condition ) { %>
<img src='<%= linkToImageSource %>' />
<% } %>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0