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"
:
<td runat="server" visible="<%# this.SomeBool %>"><t开发者_Python百科r>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.
Try something like:
<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.
精彩评论