[英]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. 我试图根据条件将标签的visible属性设置为true或false。 This is in ASPX page. 这是在ASPX页面中。 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' 编译器错误消息:CS0019:运算符'>'不能应用于'object'和'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. 当'IsAuthorized'大于零时,我需要做的就是将LABEL的visible属性设置为true。
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. 你有一个额外的>
和0
在那里。 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. 此外,由于您没有在<asp:Label
和</asp:Label>
之间执行任何操作,因此可以使用结束斜杠关闭它并跳过单独的结束标记。 Like this <asp:Label ... />
喜欢这个<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")) %>'
假设IsAuthorized
是一个位类型,只需将其IsAuthorized
转换为布尔值:
Visible='<%#Convert.ToBoolean(Eval("IsAuthorized"))%>'
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: 但是除非你在后面的代码中调用DataBind,否则它将无法工作,例如在Page_Load中:
myId.DataBind():
Assuming IsAuthorized
is an integer, you should use this: 假设IsAuthorized
是一个整数,你应该使用这个:
Visible='<%# ((int)DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>'
Eval returns an object
, so you have to cast it to an integer
first. Eval返回一个object
,因此您必须先将其强制转换为integer
。
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# (int)(DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>' ></asp:Label></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.