2

I have an Asp.NET application (VS2008, Framework 2.0). When I try to set a property on one of the user controls like

myUserControl.SomeProperty = someValue;

I get a NullReferenceException. When I debug, I found out that myUserControl is null. How is it possible that a user control handle is null? How do I fix this or how do I find what causes this?

4 Answers 4

5

Where are you trying to access the property? If you are in onInit, the control may not be loaded yet.

Sign up to request clarification or add additional context in comments.

1 Comment

Righteous. That makes perfect sense for a case I've had as well.
5

Where exactly in the code are you attempting to do this? It is possible that you are attempting to access the control too early in the page lifecycle and it has not been instantiated yet.

Comments

0

If you created the UserControl during runtime (through ControlCollection.Add), you need to create it on postback too.

Another case can be your UserControl does not match the designer.cs page

Comments

0

I was trying to set the property from markup on an outside user control. When I took the property to OnLoad, it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.