Search code examples
c#asp.netuser-controls

asp:Literal control null in user control


I've a user control which contains asp:Literal.

<div>
     <asp:Literal id="MenuContainer" runat="server" />
</div>

There is a method in code-behind page which initializes the control:

internal void Setup(MyBusinessObject obj)
{
    MenuObject menu = MenuHelper.GetMenu(obj.State);

    if(obj == null)
        MenuContainer.Visible = false;

    //other code
}

In the page where the control is used I call Setup method of control in LoadComplete event handler (I was first calling it in Load event). Irrespective of MyBusinessObject being null or not null, when I access Literal on user-control I get error:

Object reference not set to an instance of an object.

What is the reason and what is the remedy for this?


Solution

  • It was very simple. I was adding things in web.config's controls section as was suggested by Rick Sthral in one of his post ( :( for got about the post, you will have to search on his page).

    It was nicely allowing me to add controls without putting @ Register tag but the downside was that child controls on my controls were shown as null! so I simply put @ Register directive in my pages and it worked.