是什么导致标记控件为空?
好吧,我有一个非常奇怪的问题。我有一个常规的 UserControl,在标记中包含一些控件。在 Page_Load
时,这些控件仍为 null
。我尝试过 EnsureChildControls
它的布局如下:
Masterpage ->页面->我的控制1->我的控制2-> ProblemControl
ProblemControl 是控件为空的地方。 MyControl1 包含 MyControl2。 MyControl2 是另一个 UserControl,其标记中包含 ProblemControl。 Masterpage 没什么特别的,Page 的标记中包含 MyControl1。唯一奇怪的是 ProblemControl 是在 Page_Init
处动态创建的。
一切正常,直到我到达 ProblemControl,其中没有创建任何控件。 ProblemControl 已设置所有正确的内容,例如 Page 和 Parent 属性。我没有看到任何问题。所有这些(除了 ProblemControl)的源代码都非常广泛,所以我希望有人能给我一些针对这个问题的故障排除提示,以及如果有人以前遇到过它。
另外,我可以将 ProblemControl 放在另一个页面控件上,它会正常工作,这是关于 mycontrol1 和/或 mycontrol2 的事情。但我们在使用 mycontrol1 时从未遇到过任何问题,并且 mycontrol2 也没有任何我认为有问题的地方。 (过去几个小时我一直在乏味地分析)。
还有其他人遇到同样的问题吗?有什么常见的事情需要检查吗?
更新:
好的,我将在不久的将来有一个小测试用例来重现它。但目前看来,导致问题的原因似乎是在 Page_Init
处从 UserControl 内部动态创建 UserControl。我可以在 ASPX 页面上执行相同的代码,但是当我在 ASCX 控件上执行此操作时,它会出现如上所述的所有内容均为空的问题。不能嵌套UserControls吗!?
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!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后的调试技术:二分法搜索错误。
步骤1:创建一个空白控件BlankControl1。代替 MyControl1。将 MyControl2 添加到其中。有效吗?如果是这样,问题出在 MyControl2 中(进展!)。否则,问题至少部分与 MyControl1 有关。我假设情况确实如此。
步骤 2:从 MyControl1 中删除尽可能多的代码,留下包含 MyControl2 的代码。继续修改它,直到问题消失或变成 BlankControl1。在后一种情况下,有些事情很奇怪,您需要打开文件比较实用程序来找出差异隐藏的位置。
第 3 步:您现在知道如何才能解决问题。这要么导致解决方案,要么导致如何在不破坏 ProblemControl 的情况下使用引发问题的代码的问题。无论哪种方式,您都更接近解决方案。
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.
好的,我找到了答案: ASP.NET Custom user control to动态添加(请投票关闭重复项
接受的答案对我有用。最重要的是使用
Page.LoadControl("path..")
而不是 < code>Page.LoadControl(typeof(mycontrol),null) 后者导致了完全相同的事情发生,我认为他们都会做同样的事情,但显然不是。Ok, I've discovered the answer: 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 thanPage.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.