views:

267

answers:

2

I tried this, but could not get through:-

code behind

protected HtmlTableRow trComment;

protected void Page_Load(object sender, EventArgs e)
{
    //Show/Hide table rows (TR)
    trComment.Visible = ConfigUtil.DisplaySummaryComment;
}

.ascx page

<tr id="trComment" runat="server">
    <td style="vertical-align:top; text-align:left;">
        <%#ConfigUtil.FieldLabels["PIComments"]%>
        :
    </td>
    <td>
        <%= Test.Comment %>
    </td>
</tr>
A: 

Try

trComment.Style.Add("display", "none");
Bob
the `.Visible` in ASP.NET prevents rendering of that control completely. If that doesn't work, `display:none;` doesn't either.
Abel
+2  A: 

Your original code doesn't work, not because it's incorrect, but because you probably have more places with trComment (in which case it should error) or because your current code is inside a template of some sort (in a GridView, a Repeater). The latter is most likely, because you use a data-statement (<%#), which is commonly placed in a databound control template (but not necessarily).

One way to solve this uniformly and easily (many ways exist and it's probably best not to use literal tables anyway) is to use an asp:PlaceHolder, which does not leave HTML "traces", but can be used to toggle any block of HTML / ASP.NET code:

<!-- toggle through OnLoad (can use ID as well) -->
<asp:PlaceHolder runat="server" OnLoad="MakeVisibleOrNot">
    <tr>
       ...
    </
</asp:PlaceHolder>

in the code behind

protected void MakeVisibleOrNot(object sender, EventArgs e)
{
    ((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}
Abel
Awesome solution. Thanks
Novice