Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide table row <tr> in .ascx page

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>
like image 429
Jango Avatar asked Nov 09 '09 15:11

Jango


People also ask

How do you hide and show table rows in HTML?

A hidden attribute on a <tr> tag hides the table row. Although the table row is not visible, its position on the page is maintained.

How can make TD visible false in asp net?

Add a runat="server" attribute to the <td> for Column 10 and give it an ID (like id="column10Header" ). Then you can set its Visible property to false .


1 Answers

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;
}
like image 162
Abel Avatar answered Oct 02 '22 15:10

Abel