以编程方式取消控件初始化

发布于 2024-08-17 06:17:51 字数 390 浏览 0 评论 0原文

假设我有一个用户控件 MyControl.ascx,我将它放在 Default.aspx 中,如下所示:

<uc1:MyControl id="MyControl" runat="server">

现在在 Default.aspx 的代码隐藏中我这样做:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    MyControl.Visible = false;
}

问题是,即使我不渲染 MyControl,它仍然是正在初始化并经历整个 ASCX 生命周期(OnLoad 等)。有没有办法阻止控件被初始化?

谢谢。

Suppose I have a user control MyControl.ascx, and I put it in the Default.aspx like this:

<uc1:MyControl id="MyControl" runat="server">

Now in the code-behind of Default.aspx I do this:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    MyControl.Visible = false;
}

The problem is that even I do not render MyControl, it is still being initialised and goes through the entire ASCX life cycle (OnLoad, etc). Is there a way to prevent the control from being initialised at all?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱给你人给你 2024-08-24 06:17:51

与其将其添加到 aspx,不如将其动态地从代码隐藏添加到占位符。这将允许您在需要时对其进行初始化。

快乐编码。

Instead of adding it to aspx better add it to a Placeholder from code behind dynamically. This would allow you to initialise it when you require.

Happy coding.

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