tags:

views:

372

answers:

2

I have a (html)table (sample layout below) that I'd like to hide the last two columns under certain conditions, using the C# codebehind. I'd prefer not to do this by shrinking the width of the columns to 0, though I'm not ruling it out. There aren't any CSS classes really supplied to most of the rows and none of the columns. I've tried using a colgroup to set the display to none as well as the visibility to hidden on the colgroup, but with no luck.

_____________
|__|__|__|__|
|__|__|__|__|
|__|__|__|__|

to

_______
|__|__|
|__|__|
|__|__|

Any thoughts?

A: 

Where you render the header and where you render the columns, you can set Visible="false". You can set the Visible properly using code behind or with a <% (bool)Eval(WantColumnsVisible) %> in the aspx page.

Don't forget to sync all the locations that render a header or column, otherwise you'll get something rather goofy looking.

John Fisher
+1  A: 

Assuming your table is static, you could dynamically apply a CSS class to any columns you want hidden:

<table>
    <tr>
        <th>
            Visible
        </th>
        <th class="<%= HiddenClassName %>">
            Possibly hidden
        </th>
    </tr>
    <tr>
        <td>
            Visible
        </td>
        <td class="<%= HiddenClassName %>">
            Possibly hidden
        </td>
    </tr>
</table>

In the code file, your property could be:

public string HiddenClassName { get; private set; }

The hidden style itself:

<style type="text/css">
  .hidden 
  {
      visibility: hidden;
      display: none;
  }
</style>
John Rasch