如何避免在ASPX中初始化Web用户控件? [英] How to avoid Initialization of web user control inside aspx?
问题描述
我有一个aspx页面,其中包含如下所示的Web用户控件.
I have an aspx page which contains a web user control as below.
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<uc1:WebUserControl Visible="false" ID="WebUserControl1" runat="server" />
</asp:PlaceHolder>
<asp:Label ID="Label1" runat="server" Text="This is visible"></asp:Label>
</div>
</form>
</body>
</html>
在上一页的Page_Load方法中,我为WebUserControl1设置了Visible = true/false(基于某些条件).WebUserControl1本身包含许多控件.但是我不想在WebUserControl1中初始化控件.无论如何,我们可以避免初始化WebUserControl1的ChildControl吗?
In the Page_Load method of the above page I am setting the Visible = true/false (based on some condition) for the WebUserControl1. WebUserControl1 contains lots of control itself. But I don't want to initialize the controls inside WebUserControl1. Is there anyway we can avoid initializing the ChildControls of WebUserControl1?
推荐答案
根据您的描述,这听起来像是在用户控件的Init事件中完成了用户控件中的繁重工作.我是否可以建议将繁重的工作从该事件转移到Load事件.然后,如@Brian所指出的那样,您应该能够检查控件是否可见并开始进行繁重的操作.
From you description, it sounds like the heavy lifting that is being done in the user control is done in the Init event of the user control. May I suggest moving the heavy lifting out of that event to, possibly, the Load event. Then, as @Brian pointed out, you should be able to check if the control is visible and start the heavy lifting if it is.
您的用户控件的代码隐藏:
Code-Behind for your User Control:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Visible)
{
//do heavy lifting here
}
}
如果您对是否举起没有任何限制,那么您将永远举起.
If you don't put some kind of conditions on whether or not you lift, you will always lift.
这篇关于如何避免在ASPX中初始化Web用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!