Search code examples
asp.netservercontrols

Best Server Control for surrounding blocks


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.


Solution

  • 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.