Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    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


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