[英]Controlling visibility of html controls using databinding
I'm trying to control whether or not some <td>
elements are rendered or not using databinding and runat="server"
: 我试图控制是否使用数据绑定和
runat="server"
渲染某些<td>
元素:
<td runat="server" visible="<%# this.SomeBool %>"><tr>Hello world!</tr></td>
The trouble is that the SomeBool
property just isnt being called. 麻烦的是
SomeBool
属性只是不被调用。
If I explicitly set visible to false, like this: 如果我将visible显式设置为false,则如下所示:
<td runat="server" visible="False"><tr>Hello world!</tr></td>
Then all is well and the element is not rendered. 然后一切都很好,并且不呈现该元素。
How do I get this databinding to work? 如何使此数据绑定起作用?
The reason why my method wasnt being called was because the DataBind() method on my page wasn't being invoked - even just putting the following into the page somewhere did nothing: 之所以没有调用我的方法,是因为没有调用页面上的DataBind()方法-甚至只是将以下内容放入页面中也没有执行任何操作:
<%# "Hello world" %>
I had to add a call to this.DataBind()
to the top of my page: 我必须在页面顶部添加对
this.DataBind()
的调用:
<%@ Page ... %>
<% this.DataBind(); %>
And everyting then worked as expected. 然后一切都按预期工作。
尝试类似的方法:
<td <%# this.SomeBool ? "" : "style=\\"display:none;\\"" %>><tr>Hello world!</tr></td>
Try single quotes around the <% %> tags: 尝试在<%%>标记周围使用单引号:
<tr runat="server" visible='<%# this.SomeBool %>'><td>Hello world!</td></tr>
Sergio's idea looks neat, too. 塞尔吉奥的想法看起来也很整洁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.