以编程方式取消控件初始化
假设我有一个用户控件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其将其添加到 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.