Mario Levrero Mario Levrero - 3 months ago 8
ASP.NET (C#) Question

When a control won't be rendered, why are its properties evaluated?

Having a nested control under a

Visible=false
, why ASP.NET trying to compile it?

The Control.Visible Property documentation says:


If a container control is not rendered, any controls that it contains
will not be rendered


My CodeBehind:

public MyClass myVariable = null;


My view code is:

<asp:panel runat="server" Visible="false">
<img id="imgNotConfirmedPhoto" src="<%# myVariable.Whatever %>" />
</asp:panel>


It throws an exception:


Object reference not set to an instance of an object


when tries to compile
myVariable.Whatever
. Why is ASP.NET trying to evaluate the image
src
if the container has
Visible=false
?

Answer

As you can see in MSDN: ASP.NET Page Life Cycle Overview, the entire page will be parsed and loaded long before they will be rendered.

This includes controls that won't be visible after rendering (for example when they, or their parent controls have Visible = false).

So whether a control will be rendered or not, it will be parsed and loaded, and its properties will be evaluated, causing the exception you see.

Comments