views:

253

answers:

2

Does a custom control (ascx) get rendered regardless of whether visibility property is true or false.

It seems that when some controls have their visibility set to false, they do not get rendered, yet when I set an ascx control to be hidden from the aspx page that contains it, the code still runs through the ascx's codebind, despite it being marked as not visible.

Is there a way to force my ascx not to render if visibility is marked false?

Is this behavior normal?

+1  A: 

The code behind is always executed regardless if the control is visible or not.

Consider the case when a control is hidden by default, but is made visible by code during the execution of the page. If the constructor and Load event of the control would be suppressed because it was hidden, it would not be properly initialised. If the events were executed at the moment the control was made visible, they would execute out of order in the page cycle.

So, the code behind of all controls in the page has to run always, as the control may be visible when it comes to rendering time.

Guffa
+1  A: 

If visibility is set to false, the ascx still goes through the page life cycle. No HTML will be generated, so in that respect it is not rendered. The Load event runs, but pre-render probably does not.

Cylon Cat