使用嵌套母版页时用户控件实例为空

发布于 2024-11-27 18:09:53 字数 348 浏览 1 评论 0原文

我有几个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-12-04 18:09:53

博客文章 很好地描述了问题并提供了解决方案。我可以确认该解决方案有效,我将在此处重复相关部分:

问题

我们有一个用户控件,并在 Init 事件中初始化其控件,以便它当 ViewState 恢复时(InitLoad 之间)准备就绪。

一旦您开始使用这种封装技术,很快您就会想要传入一个影响您加载的数据的参数。在此之前,我们需要知道 Init 事件是按相反顺序触发的。也就是说,子控件的 Init 事件先于父控件触发该事件。因此,Page.Init 事件对于我们来说已经太晚了,无法在控件上设置任何属性。

自然的解决方案是尝试使用 Page.PreInit 事件,但是当您这样做时,您经常会发现控件引用都是null。当您的页面是使用母版页实现时,就会发生这种情况,并且它与母版页的实现方式有关。母版页中的 控件使用 ITemplate 接口来构建其内容。此内容(子控件)通常在调用 Init 事件之前不会准备好,这意味着控件引用不可用。对于我们来说,这代表了一个问题。

解决方案

修复方法非常简单;我们需要做的就是触摸 Page 上的 Master 属性,这将使控件变得可用。如果我们使用嵌套母版页,我们需要接触链中的每个母版页。

然后,该博客文章的作者提供了一个不错的小片段,您可以在 PagePreInit 处理程序中执行该片段,该处理程序使用 MasterPage 并包含用户控件:

protected override void OnPreInit(EventArgs e)
{
    // Walk up the master page chain and tickle the getter on each one
    MasterPage master = this.Master;
    while( master != null ) master = master.Master;
    
    // Access now initialized user control
    ucAddress.City = "Dallas";
}

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 the ViewState is restored (between Init and Load).

Once you start using this encapsulation technique, it won’t be long until you want to pass in a parameter that affects the data you load. Before we do, we need to be aware that the Init event is fired in reverse order. That is, the child controls have their Init event fired before that event is fired at the parent. As such, the Page.Init event is too late for us to set any properties on the controls.

The natural solution is to try and use the Page.PreInit event, however when you do you’ll often find that your control references are all null. This happens when your page is implemented using a master page, and it relates to how master pages are implemented. The <asp:ContentPlaceHolder /> controls in a master page use the ITemplate interface to build their contents. This content (child controls) is not usually prepared until the Init event is called, which means the control references are not available. For us, this represents a problem.

The Solution

The fix is remarkably simple; all we need to do is touch the Master property on our Page and it will cause the controls to become available. If we are using nested master pages, we need to touch each master page in the chain.

The author of the blog post then offers a nice little snippet that you can execute in the PreInit handler of your Page that uses a MasterPage and contains a user control:

protected override void OnPreInit(EventArgs e)
{
    // Walk up the master page chain and tickle the getter on each one
    MasterPage master = this.Master;
    while( master != null ) master = master.Master;
    
    // Access now initialized user control
    ucAddress.City = "Dallas";
}
黒涩兲箜 2024-12-04 18:09:53

发现问题了。在访问用户控件之前必须初始化子母版页。

Found the issue. had to initialize child master page before accessing user control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文