views:

91

answers:

4

I am wondering what server control people generally use for surrounding and separating blocks of related controls and content in a Web Forms scenario. Often times I need to surround a block of HTML and related Server controls with a 'panel' that I can show or hide based upon some UI logic in the code behind. I am usually faced with either using a Panel, however this doesn't work if I need to surround multiple TR's in a table for example because the Panel renders as a div which is invalid inside a Table tag. In this situation I usually just set each TR to runat server and show/hide each of them individually, but there must be a better way. I have seen the PlaceHolder control used for this scenario, but wanted to know if anyone does this or has any other suggestions.

A: 

Even with the PlaceHolder control, Visual Studio will complain that you don't have valid HTML.

You might grit your teeth and do it via server side code blocks that access properties on the code behind:

<table>
  <tr>
     ....
  </tr>

  <% if( ShowSecretRow ) { %>

  <tr>
    <td>shhh!</td>
  </tr>

  <% } %>
</table>

Be careful though... it's a slippery slope and you don't want your ASPX to be too convoluted.

Ben Scheirman
A: 

Perhaps a literal control.

Joel Coehoorn
Correct me if I'm wrong, but I don't think you can put surround HTML on a literal unless you set it in the text property. I think if you try to surround a bunch of HTML in the .ascx or .aspx with a <asp:literal ...>HTMLHERE </asp:literal> you get an error.
Yobi21
A: 

You can use any html element you want, just give it an id and runat server. That gives you some flexibility.

Mark Brackett
Yes, but I am interested in a control that doesn't render as any HTML. I know I can use any HTML control, but in the example of showing/hiding multiple tr's in a table I would like just one control to set visibility not each tr.
Yobi21
+1  A: 

I don't think there is a single answer that is true for all scenarios - we don't have hundreds of tools so that we can always use a hammer.

Panels are great for when a resulting div will work just fine. PlaceHolder inherits from Control instead of WebControl so you could make the argument that it uses less overhead. When I have a table and need to hide an individual table row, I have no compunctions with adding and id and runat="server" to the tr either.

Or, for times when I need to make a great big table of editor controls (this fits 90% of the times when I would want to hide a single table row) I've even made a control that automates some of the markup for me. That control automates the field header text, whether or not to make it bold (required), adds a help bubble with rollover tooltip, and all I have to add manually is the text box or other editor control. The positive side effect is that my custom control gives me an ID I can set Visible on as well.

David