简体   繁体   English

ASP.NET用户控件:检查用户控件是否可见

[英]ASP.NET usercontrol: check if usercontrol is visible

My question is somewhat related to this thread: How to avoid Initialization of web user control inside aspx? 我的问题与此线程有关: 如何避免在aspx中初始化Web用户控件?

The UserControl should only be visible when a button is clicked. 仅当单击按钮时,UserControl才可见。 So my usercontrol is set like this: 所以我的用户控件设置如下:

<example:usercontrol ID="idControl" runat="server" Visible="false" />
<asp:Button ID="showMyControl" runat="server" OnClick="ShowMyControl" /

And the usercontrol itself checks if he's visible: 用户控件本身会检查他是否可见:

protected void Page_Load( object sender, EventArgs e ) {
    // only execute if the user control is visible
    if ( this.Visible ) {
        ...
    }
}

When I click the button I'm setting the property Visible of the usercontrol to true but when the postback occurs the Visible-property in the Page_Load of the usercontrol is still set to false. 当我单击按钮时,我将usercontrol的Visible属性设置为true,但是发生回发时,usercontrol的Page_Load中的Visible-property仍然设置为false。

My UserControl is probably loaded first and the Visible-property is set afterwards? 我的UserControl可能会先加载,然后再设置Visible-property? Is there an easy way to resolve this? 有解决此问题的简便方法吗?

Thanks in advance! 提前致谢!

The Load event of your control, which is being handled by your Page_Load event handler method if I understand correctly, gets fired before the Click event of your button control. 如果我理解正确,则由Page_Load事件处理程序方法处理的控件的Load事件在按钮控件的Click事件之前触发。 Therefore, when the Page_Load method checks this.Visible , the property has not yet been changed because the Click event handler has not yet executed. 因此,当Page_Load方法检查this.Visible ,该属性尚未更改,因为尚未执行Click事件处理程序。

For this reason, I think that checking the Visible property of your control would be more appropriate in the PreRender event instead of the Load event. 因此,我认为在PreRender事件而不是Load事件中检查控件的Visible属性更为合适。

I'm guessing that you're doing some sort of data retrieval or something that you wish to avoid if the control is not visible. 我猜测您正在执行某种数据检索,或者如果控件不可见,则希望避免这种情况。 Unfortunately, this sort of issue regarding the page life-cycle and the order in which events fire is sort of a common issue to deal with in ASP.Net programming. 不幸的是,这种与页面生命周期和事件触发顺序有关的问题是ASP.Net编程中要解决的常见问题。

If all of your initialization code can easily be moved into the PreRender event, then great. 如果所有初始化代码都可以轻松移入PreRender事件,那就好了。 Problem solved (hopefully). 问题已解决(希望如此)。 If not (ie you need something to happen before the PreRender), you may need to come up with some other mechanism to ensure that your code executes at the right time. 如果不是这样(即您需要在PreRender之前进行一些操作),则可能需要提出一些其他机制来确保您的代码在正确的时间执行。 For example, you could expose a "SetVisible" method on your control which sets the Visible property to true and then also executes whatever initialization logic is needed. 例如,您可以在控件上公开“ SetVisible”方法,该方法将Visible属性设置为true,然后执行所需的初始化逻辑。 The downside of this is that you can't really guarantee that some code won't just set your control's Visible property to true outside of the SetVisible method that you provide. 这样做的缺点是您不能真正保证某些代码不会仅仅在您提供的SetVisible方法之外将控件的Visible属性设置为true。

Another idea is to actually override the Visible property and perform your initialization logic whenever that property gets set to true. 另一个想法是实际上重写Visible属性,并在该属性设置为true时执行初始化逻辑。

Put the code which you call in the Control's Page_Load event into the PostBack event. 将您在控件的Page_Load事件中调用的代码放入PostBack事件中。 Make sure the button has autopostback set to true. 确保按钮的autopostback设置为true。

What you are looking for is the Control.EnsureChildControls method. 您正在寻找的是Control.EnsureChildControls方法。 This method exists for this very situation. 在这种情况下存在此方法。 It will ensure that all child controls have been created. 这将确保已创建所有子控件。 Then you can set your visible property. 然后,您可以设置可见属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM