Set value for 'visible' property in ASPX page programatically

Anirudh picture Anirudh · Oct 27, 2011 · Viewed 17.7k times · Source

I am trying to set the visible property for a label to either true or false depending on a condition. This is in ASPX page. I am doing something wrong and getting error when this is executed.

<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED'
   Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") > 0%>'>
</asp:Label></td>

Error I am getting is below.

Compiler Error Message: CS0019: Operator '>' cannot be applied to operands of type 'object' and 'int'

What changes need to be done?

All I need to do set the visible property of the LABEL to true when 'IsAuthorized' is greater than zero.

Answer

rlb.usa picture rlb.usa · Oct 27, 2011

That's because you have a syntax error, you silly bunny.

Here you are, it should be like this:

 <td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") %>'  /></td>

You had an extra > and a 0 in there somewhere. Also, since you aren't doing anything between the <asp:Label and </asp:Label>, you can close it with an end slash and skip a separate ending tag. Like this <asp:Label ... />

ALSO, sometimes trying to set a visible property like that causes problems, the program can complain that the value wasn't a Boolean. You might want to also ad an explicit conversion like this:

 Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsAuthorized")) %>'