Reputation: 40527
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?
Upvotes: 5
Views: 7250
Reputation:
Thanks, TheVillageIdiot, for posting the answer to your problem - I ran into exactly the same misunderstanding.
Adding controls via
<add tagPrefix="user" namespace="Frontend.Web.UserControlsAccount" assembly="Frontend.Web" />
in the web.config was not enough to actually use it! I tried it like this on a page:
<user:ucLoginMessages runat="server" ID="Msgs" />
... but this would lead to the phenomenon, that none of the controls inside the UserControl were initialized. Only adding
<%@ Register Src="~/UserControlsAccount/LoginMessages.ascx" TagPrefix="user" TagName="Messages" %>
to the top of the page solved the problem :-)
Thanks again!
Upvotes: 1
Reputation: 107606
It depends on exactly how you're including the controls in your web.config. It will not work if you try to include all of the controls in a namespace (although the designer will correctly show you the list of your controls):
<add tagPrefix="prefix" namespace="example.ui.controls" assembly="example.ui" />
But if you add your control(s) individually and point to their physical location(s), it will work as you expect without having to include endless @Register
directives.
<add tagPrefix="prefix" tagName="Message" src="~/Controls/Message.ascx" />
Upvotes: 5
Reputation: 40527
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.
Upvotes: 11
Reputation: 124794
The code you posted is the following:
internal void Setup(MyBusinessObject obj)
{
MenuObject menu = MenuHelper.GetMenu(obj.State);
if(obj == null)
MenuContainer.Visible = false; //other code
}
If obj is null, then dereferencing obj.State on the first line will throw a NullReferenceException
If obj is not null, the line MenuContainer.Visible = false won't be executed.
So I don't think you're posting all the relevant code.
When you're having difficulty debugging this kind of thing, try stepping through the code with the debugger or adding some asserts to your code, which will help you to see exactly what's happening:
internal void Setup(MyBusinessObject obj)
{
Debug.Assert(obj != null);
MenuObject menu = MenuHelper.GetMenu(obj.State);
Debug.Assert(MenuContainer != null);
if(obj == null)
MenuContainer.Visible = false; //other code
}
Upvotes: 0
Reputation: 5917
If MenuContainer
is null, it probably has something to do with the time line of the page life cycle. You're calling that function before MenuContainer
is linked up. Can you try calling Setup
in the Page_Load function?
Upvotes: 0
Reputation: 21685
As mentioned in the answer by JerSchneid, if obj is null, you'll get that error. So, try doing it like this -
internal void Setup(MyBusinessObject obj)
{
if(obj == null)
MenuContainer.Visible = false;
else
MenuObject menu = MenuHelper.GetMenu(obj.State);
}
EDIT: I know you are getting an error on that line, but just try doing it like this. Or, else, remove the whole code and just keep the MenuContainer.Visible = false;
line.
Upvotes: 0
Reputation: 5917
Are you sure MenuContainer
is the problem? You are referencing obj.State
in the first line of the Setup
function. If that obj
is null you'll get that error.
Upvotes: 0