简体   繁体   中英

Web forms prevent Page_Load event in control when inside placeholder that visible false

i need to prevent UserControl Page_Load event in case the control placed inside PlaceHolder that Visible property set to false. I have some base class that all my user control derived from, and this class derived from UserControl class. I found this : How to stop the execution of UC at page load on Visible false The answer was to use "this.Visible" inside the Page_Load event handler Or to override OnPreRender method and use it instead of the Page_Load.

I need some way to solve this problem inside my BaseControl, in order to avoid multiple code changes. Is it possible?

Thanks in advance!

There is nothing you can do to circumvent the execution of Page_Load. This event will be raised regardless because it is part of the page life cycle. What you can do however, is to conditionally execute any heavy logic based on this.Visible.

If you want to check visibility of a child control from a base class, and then conditionally handle additional shared logic there (whatever that may be), you can do something like this:

// DerivedChildControlA.ascx
<uc1:ChildControl runat="server" ID="someChildControlID" />




public abstract class BaseControl : UserControl
{
    protected abstract ChildControl DerivedChild { get; }
}

public class DerivedChildControlA : BaseControl
{
    protected override ChildControl DerivedChild
    {
        get { return this.someChildControlID; }
    }
}

Once you obtain the reference to that child, you can check its visibility and perform desired actions in the base class.

For example, what I normally do for all my UserControls is put the main loading logic in a separate method called Load(). Then I call this method from the parent. The thing is, you can conditionally call Load() which gives you more control. In your instance, you can call Load() from the parent base class depending on the child control's visibility.

EDIT:

There may be a way by removing the child control from the page's child control collection, but this feels like a hack. For additional info check out the comment by Aterra on ASP.NET Forums.

You could load the User Controls dynamically. Then you won't have the problem of them being loaded even though they are not visible. So on the aspx page you can do this:

if (showControl == true)
{
    //create an instance of the user control
    WebUserControl1 control1 = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");

    //add it to the page when needed
    PlaceHolder1.Controls.Add(control1);
}

The only downside is that the Control will be gone after each PostBack, so you will have to keep track if the Control was shown somewhere and recreate it if needed.

Another thing you can do is leave the Page_Load of the User Control empty and create a Method that you call from the parent page.

User Control

protected void Page_Load(object sender, EventArgs e)
{
    //empty
}

public void doStuffInUserControl()
{
    Label1.Text = "Called from parent!";
}

Parent aspx page

protected void Page_Load(object sender, EventArgs e)
{
    someChildControlID.doStuffInUserControl();
}

Finally i found a solution:

public abstract class BaseClass : UserControl
{
    protected override void OnLoad(EventArgs e)
    {
        if (this.Visible)
        {
            base.OnLoad(e);
        }
    }
}

public partial class WebUserControl1 : BaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // running only if this.Visible = true
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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