Set value for 'visible' property in ASPX page programatically
Asked Answered
L

5

5

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.

Lurleen answered 27/10, 2011 at 19:11 Comment(0)
M
12

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")) %>' 
Main answered 27/10, 2011 at 19:15 Comment(1)
This is a beautiful answer! From silly bunny, to the corrected line of code, to the explanation of the correction, and then at the end "you might also want to". This is the perfect formula for an answer - 1) answer, 2) explain, 3) improve.Guyon
C
5

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

 Visible='<%#Convert.ToBoolean(Eval("IsAuthorized"))%>'  
Cageling answered 27/10, 2011 at 19:26 Comment(2)
And remember it can't be NULL. :)Tesler
Thanks much. This worked great for GridView with an XML source. <asp:Label ID="hll1" Visible='<%# Convert.ToBoolean(Eval("srcvis1")) %>' Text="<br />" runat="server" /> The key of course was to replace the Bind() call with the Eval() call.Severus
F
2

Note on a server side control you can do this:

<someControl id="myId" runat="server" Visible='<%# this.SomeField > 5 %>'>

But it won't work unless you call DataBind in the code behind, such as in Page_Load:

myId.DataBind():
Fulgent answered 9/1, 2013 at 18:7 Comment(0)
A
1

Assuming IsAuthorized is an integer, you should use this:

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

Eval returns an object, so you have to cast it to an integer first.

Anthea answered 27/10, 2011 at 19:17 Comment(1)
Seems that with the "> 0" included by the OP, this is the correct answer.Foch
M
0
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# (int)(DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>' ></asp:Label></td>
Mantellone answered 27/10, 2011 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.