如何在页面加载时停止UC执行Visible false [英] How to stop the execution of UC at page load on Visible false

查看:122
本文介绍了如何在页面加载时停止UC执行Visible false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果容器容器上的UC设置为Visible false,我正在考虑停止执行页面加载.现在我要遵循以下逻辑

public class TestControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Visible)
        {
            //do heavy operation

        }
    }
}

即我将检查该控件在UC的Page加载中是否可见,如果为true,则仅在其中进行操作.还有其他更好的方法吗?我的应用程序中有很多UC.如果它是Visible false,是否有一些通用的逻辑来停止执行UC的页面加载.

解决方案

Control类的Visible属性仅确定是否呈现了Control,尽管它对控件的整个生命周期没有影响.

来自 MSDN :

如果此属性为false,则不呈现服务器控件.在组织页面布局时,应考虑到这一点.如果未呈现容器控件,则即使将单个控件的Visible属性设置为true,也不会呈现其包含的任何控件. [...]

这是有道理的,因为那样您就可以拥有仍执行特定操作的不可见控件.您也可以通过不添加任何内容来做到这一点,但是它们可能仍会呈现一些空白或换行符.将Visible设置为false是一种干净的方法.

如果控件不可见,我认为没有更好的方法来阻止代码执行,但是我认为您的解决方案足够干净.

作为替代方案,您可以将代码放入 OnPreRender 事件中,该事件仅在控件可见时才触发.但是请记住,这是控制生命周期中的后续步骤. >

public class TestControl : System.Web.UI.UserControl
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        // Your code here
    }
}

I am thinking to stop the execution of pageload if that UC has been set to Visible false at the container page. Right now I am upto the following logic

public class TestControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Visible)
        {
            //do heavy operation

        }
    }
}

i.e I will check whether the control is visible false or not in the Page load of UC, if true, then only do the operations there. Is there any other better way to do so? I have so many UC in my application. Is there some common logic to stop the execution of page load of UC if it is Visible false.

解决方案

The Visible property of the Control class only determines if the Control is rendered, it has no effect on the rest of a control's life cycle though.

From MSDN:

If this property is false, the server control is not rendered. You should take this into account when organizing the layout of your page. If a container control is not rendered, any controls that it contains will not be rendered even if you set the Visible property of an individual control to true. [...]

This makes sense, because that way you can have invisible controls that still perform a certain action. You could also do this by just not putting any content into them, but they probably still will render some whitespace or newline. Setting Visible to false is the clean way to do it.

I don't think there is any better way to prevent the execution of code if the Control is not visible, but I think you're solution is clean enough.

As an alternative you could put your code into the OnPreRender event, which is only fired when the control is visible. But keep in mind that this is a later step in the Control Lifecycle.

public class TestControl : System.Web.UI.UserControl
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        // Your code here
    }
}

这篇关于如何在页面加载时停止UC执行Visible false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆