0

When we design the Page in ASP.NET Is it possible to remove the unused controls from codebehind instead of hiding them? Because, hiding them from codebehind makes them invisible but do not remove completely resulting in whitespaces on user screens.

for eg:-

<td>
<uc1:GoToRequestControl id="UserControl" runat="server" />    

we try to show/hide form Codebehind as follows

if (Condition)
{
    UserControl.Visible = false;
}

This hides the control but the space for this control stays on the Page. Is there a way we can remove the whitespacing for these hidden controls and make the page look better? We are using tables to define the layout, Is this the reason for the above behavior?

Programmerzzz
  • 1,237
  • 21
  • 48
  • The whitespaces you're talking about probably exists before/after the control as the Render method for your user control will not fire if it isn't Visible. Could those be the line breaks? Try this: `
    []
    `
    – Rubens Farias Sep 02 '15 at 00:04

1 Answers1

0

It is true that setting Control.Visible = false; will cause nothing to be rendered for that control, whitespace (and other text) around controls is not part of the control's generated markup and so will continue to be rendered.

However, whitespace should not be significant to web user-agents (i.e. browsers) unless you have CSS rules set-up that make whitespace visible.

Create or modify your CSS rules to change the way white-space and line-break are configured so the effect of extra whitespace is nullified.

Another option is to simply remove whitespace around your controls:

<pre>

<asp:Label runat="server" />

</pre>

Becomes:

<pre><asp:Label runat="server" /></pre>
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Isn't this more about `visibility: hidden` vs. `display: none`. I haven't done WebForms in quite some time, so I'm not sure about the behaviour of `Control.Visible`? – Brendan Green Sep 02 '15 at 00:29
  • Thats interesting to see what difference does it make for `visibility: hidden `vs. `display: none`. If my User control is designed by table and by chance have any line breaks they will be removed. What about empty in user control will they be removed too if enclose din
     tag?
    – Programmerzzz Sep 02 '15 at 00:34
  • @BrendanGreen @Programmerzzz see here: http://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone - and `Control.Visible = False` means that nothing will be rendered for that control in markup. – Dai Sep 02 '15 at 21:01
  • @Programmerzzz you cannot put a table in a `
    ` element, and `` must be a child of a table.
    – Dai Sep 02 '15 at 21:02