Search code examples
c#asp.netpage-lifecycleinline-code

ASPX in-line code evaluation when parent container is not visible


given the following in-line aspx c# code, if the parent panel (pnlX) had its visibility property set to false in the page load event, would the in-line code be hit?

<asp:Panel ID="pnlX" runat="server">
    <h1>Value is: <%= objectX.prop %></h1>
</asp:Panel>

The reason i ask is that there is some conditional logic (below) where one path sets the objectX object to something. The other path does not and at the same time sets plnX.Visibility to false. My problem is that i am still getting null reference exceptions on objectX.

objectX = null;
if (true)
{
    objectX = something..
}
else
{
    pnlX.Visible = false;
}

Ta


Solution

  • Have you tried declaring the Panel with Visible="false" in the .aspx file, and then, in your code, instead of setting Visible to false if you don't need the panel, set it to true if you do? I believe that will get around your problem of the null reference.