Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value for 'visible' property in ASPX page programatically

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.

like image 373
Anirudh Avatar asked Oct 27 '11 19:10

Anirudh


2 Answers

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")) %>' 
like image 123
rlb.usa Avatar answered Nov 09 '22 01:11

rlb.usa


Assuming that IsAuthorized is a bit type, just cast it to a boolean:

 Visible='<%#Convert.ToBoolean(Eval("IsAuthorized"))%>'  
like image 35
James Johnson Avatar answered Nov 09 '22 03:11

James Johnson