Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
If you use for example a Repeater and you do not want to show some field from the DataSource based on a expression, you could use the following solution. This example hides the year when the year in the DataSource is zero.
<asp:Repeater runat="server" id="rptGroup"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr><td> <asp:Label id="lbl" runat="server" Visible='<%#DisplayYear(DataBinder.Eval(Container.DataItem, "year"))%>'> <%#DataBinder.Eval(Container.DataItem, "year")%> </asp:Label> </td></tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate></asp:Repeater>
In the code-behind the following method is added:
public bool DisplayYear(object item){ int year = 0; try { year = Convert.ToInt32(item); } catch(exception) { }
return year != 0;}