Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Hiding a Table Column Using ASP.net
Date 7/7/2010    Tags ASP.net    (0)

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.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)