views:

41

answers:

2

Ok, I have a very strange problem. I have a regular UserControl with some controls in the markup. At Page_Load these controls are still null. And I have tried EnsureChildControls

It is laid out like this:

Masterpage -> Page -> mycontrol1 -> mycontrol2 -> problemcontrol

ProblemControl is where the controls are null. MyControl1 contains MyControl2. MyControl2 is another UserControl which contains ProblemControl in it's markup. Masterpage is nothing special and Page contains MyControl1 in it's markup. The only oddity is that ProblemControl is created dynamically at Page_Init.

Everything works fine until I get to ProblemControl where none of the controls are being created. ProblemControl has the proper things all set such as the Page and Parent property. I do not see any problems. The source code for all of these(except ProblemControl) is pretty extensive, so I'm hoping someone can just give me some troubleshooting tips for this problem and if anyone has encountered it before.

Also, I can place ProblemControl on another Page control and it will work fine, it's something about mycontrol1 and/or mycontrol2. But we've never had any problems with mycontrol1 and mycontrol2 doesn't have anything I can see wrong with it. (which I've been tediously analyzing for the past few hours).

Has anyone else had this same problem? Are there any common things to check for?

Update:

Ok, I will have a small test case in the very near future to go along with this to reproduce it. But for right now, it seems that creating a UserControl from inside a UserControl dynamically at Page_Init is what causes the problem. I can do the same code on a ASPX page, but when I do it on an ASCX control it breaks with everything being null problem as described above. Can you not nest UserControls!?

A: 

Last resort debugging technique: Binary search for bug.

Step 1: Create a blank control BlankControl1. in place of MyControl1. Add MyControl2 to it. Does it work? If so, the problem is in MyControl2 (progress!). Otherwise, the problem is at least partially involved with MyControl1. I will assume this is the case.

Step 2: Delete as much code as possible from MyControl1, leaving behind the code that contains MyControl2. Keep modifying it until either the problem goes away or it becomes BlankControl1. In the latter case, something is weird, and you'll want to open up a file comparison utility to figure out where the differences are hiding.

Step 3: You now know what it takes to make the problem go away. Either this leads to a solution, or this leads to a question of how to use the problem-inducing code without breaking ProblemControl. Either way, you're much closer to a solution.

Brian
aw :( I dislike this sorta debugging.. but guess I'll have to do it tomorrow at work..
Earlz
A: 

Ok, I've discovered the answer: http://stackoverflow.com/questions/2275625/asp-net-custom-user-control-to-add-dynamically (please vote to close as duplicate

The accepted answer there is what worked for me. The most important thing is to use Page.LoadControl("path..") rather than Page.LoadControl(typeof(mycontrol),null) The later caused the exact same thing to happen. I would think they both would do the same thing, but apparently not.

Earlz