Mario Levrero Mario Levrero - 1 year ago 66
ASP.NET (C#) Question

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

Having a nested control under a

, 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 %>" />

It throws an exception:

Object reference not set to an instance of an object

when tries to compile
. Why is ASP.NET trying to evaluate the image
if the container has

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download