I’m still getting used to the way ASP.net WebForms handles things but this one is still puzzling to me. In some instances I have markup that should be displayed if an object is null and that markup should contain values from that object.
A very simple example:
<asp:PlaceHolder runat="server" Visible='<%# myObject != null %>'>
<span><%= myObject.Property %></span>
</asp:PlaceHolder>
The problem is that it seem ASP.net parses the contents of the placeholder regardless of the visibility. The above code ends with the error:
Object reference not set to an instance of an object.
Is there a way to handle this without having a million <asp:Literal>‘s?
Set the visibility of the placeholder server side (i.e in your code behind)
example: