views:

93

answers:

3

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?

+1  A: 

Try something like:

<td <%# this.SomeBool ? "" : "style=\"display:none;\"" %>><tr>Hello world!</tr></td>

Sergio
+1  A: 

Try single quotes around the <% %> tags:

<tr runat="server" visible='<%# this.SomeBool %>'><td>Hello world!</td></tr>

Sergio's idea looks neat, too.

Jeremy McGee
+3  A: 

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.

Kragen