使用嵌套母版页时用户控件实例为空
我有几个在 aspx 中静态引用的用户控件。我们在 Page_Preinit 事件中的用户控件上设置一些公共属性。 该页面还引用了母版页,
到目前为止,该页面运行良好。必须重新设计用户界面,我们实现了嵌套母版页。
现在,突然间,用户控件显示为空。如果我将母版页更改为父级页面(而不是子级嵌套),则它可以正常工作。
感谢对此问题的任何指示。
一些示例代码:这里 ucAddress 为 null
protected void Page_PreInit(object sender, EventArgs e) { ucAddress.City = "Dallas"; }
I have couple of user controls which are statically referenced in aspx. We are setting some public properties on the user controls in Page_Preinit event.
The page also references a master page
This was working fine so far. Had to do a ui redesign and we implemented nested master pages.
Now, all of a sudden, the user controls are showing up as null. If I change the master page to parent one (instead of child -nested), it works fine.
Appreciate any pointers to this issue.
some sample code: here ucAddress is null
protected void Page_PreInit(object sender, EventArgs e) { ucAddress.City = "Dallas"; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此博客文章 很好地描述了问题并提供了解决方案。我可以确认该解决方案有效,我将在此处重复相关部分:
问题
我们有一个用户控件,并在
Init
事件中初始化其控件,以便它当ViewState
恢复时(Init
和Load
之间)准备就绪。解决方案
然后,该博客文章的作者提供了一个不错的小片段,您可以在
Page
的PreInit
处理程序中执行该片段,该处理程序使用MasterPage
并包含用户控件:This blog post describes the problem very well and also offers a solution. I can confirm that the solution works and I'll repeat the relevant parts here:
The problem
We have a user control and initialize its control in the
Init
event so that it is ready when theViewState
is restored (betweenInit
andLoad
).The Solution
The author of the blog post then offers a nice little snippet that you can execute in the
PreInit
handler of yourPage
that uses aMasterPage
and contains a user control:发现问题了。在访问用户控件之前必须初始化子母版页。
Found the issue. had to initialize child master page before accessing user control.