Hiding a Table Column Using ASP.net
Recently I found the need to hide a full column in an HTML table based on some information determined on the server. This turns out to be rather straightforward to do but since I had a difficult time locating code to do just that, I figured I'd share.
First off, you need to add an ID and the runat attribute to you table so that you can access them server-side.
<table id="tblMyTable" runat="server" >
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
Now in your code behind, just do a simple loop through each of the rows and hide the appropriate cells (hiding the same cell in each row effectively creates a hidden column). Assuming you want to hide the first column in your table (zero indexed), the code would look a little something like this.
foreach (HtmlTableRow thisRow in this.tblMyTable.Rows)
{
thisRow.Cells[0].Visible = false;
}
Here's one gotcha to watch out for... HTML tables are just HTML tables. Adding the runat attribute allows you to manipulate them server-side but you'll get an error if your table includes an ASP.net controls. If that is the case, you'll have to do something else such as using an ASP table object instead.
/tds/g
This article has been view 4041 times.
|