I'm trying to control whether or not some <td>
elements are rendered or not using databinding and runat="server"
:
<td runat="server" visible="<%# this.SomeBool %>"><tr>Hello world!</tr></td>
The trouble is that the SomeBool
property just isnt being called.
If I explicitly set visible to false, like this:
<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:
<%# "Hello world" %>
I had to add a call to this.DataBind()
to the top of my page:
<%@ 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.