How do I set the Visible attribute in an ItemTemplate?
-
20-09-2019 - |
Question
<asp:TemplateField HeaderText="Audio">
<ItemTemplate>
<asp:Image ID="playImage" runat="server"
ImageUrl="~/images/nextpg.gif"
Visible='<%# (Eval("available")=="Y") ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
In my query I am returning the "available" column which is populated with a letter of Y or N. For some reason the evaluation of this expression is never true. If I change it to !=
instead of ==
it will always be true. That leads me to believe the Eval("available")=="Y"
is simply not evaluating as expected.
Solution
After much messing around, this finally worked:
<%# ((String)Eval("available")).Equals("Y") ? true : false %>
The issue seems to be that you can't use ==
but instead you must use the String.Equals()
method. I'm not sure why but that's just the way it is.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow