简体   繁体   English

Web表单在占位符内显示为false时阻止控件中的Page_Load事件

[英]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. 我需要防止UserControl Page_Load事件,以防将Visible属性设置为false的PlaceHolder中的控件放置。 I have some base class that all my user control derived from, and this class derived from UserControl class. 我有一些基础类,我的所有用户控件都派生自该类,而该类又是从UserControl类派生的。 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. 我发现了这一点: 如何在Visible false上的页面加载时停止执行UC答案是在Page_Load事件处理程序中使用“ this.Visible”,或者重写OnPreRender方法并使用它而不是Page_Load。

I need some way to solve this problem inside my BaseControl, in order to avoid multiple code changes. 我需要某种方法来解决BaseControl内部的此问题,以避免多次代码更改。 Is it possible? 可能吗?

Thanks in advance! 提前致谢!

There is nothing you can do to circumvent the execution of Page_Load. 您无法做任何事情来规避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(). 例如,通常我对所有UserControl所做的工作都是将主加载逻辑放在称为Load()的单独方法中。 Then I call this method from the parent. 然后我从父级调用此方法。 The thing is, you can conditionally call Load() which gives you more control. 关键是,您可以有条件地调用Load(),从而为您提供更多控制权。 In your instance, you can call Load() from the parent base class depending on the child control's visibility. 在您的实例中,您可以根据子控件的可见性从父基类调用Load()。

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. 有关其他信息,请查看Aterra在ASP.NET论坛上的评论

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: 因此,在aspx页面上,您可以执行以下操作:

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. 您可以做的另一件事是将用户控件的Page_Load保留为空,并创建一个从父页面调用的方法。

User Control 用户控制

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

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

Parent aspx page 父aspx页面

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
    }
}

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

相关问题 Visible =&#39;&lt;%#UserCanEdit%&gt;&#39;–如果在Page_Load中设置为true,则控件不可见 - Visible='<%# UserCanEdit %>' – if set to true inside Page_Load, then control isn't visible 在Page_Load事件中进行表单身份验证 - Forms Authentication in Page_Load Event 无法在Web Control的Page_Load事件中访问Public方法 - Cannot access Public method in Web Control's Page_Load event 如何在Page_Load事件上找到位于数据列表itemtemplate内的控件? - How to find a control located inside datalist itemtemplate, on a Page_Load event? Page_Load事件中的异步用户控件加载 - Async User Control load in Page_Load event 只有在以下情况下,EditItemTemplate内部的控件才可以在Page_Load()中使用: - Control inside EditItemTemplate will be available in Page_Load() only if 在page_load上未创建任何控件 - No control created on page_load ASP.NET将UserControl动态添加到PlaceHolder,不触发Click事件,仅触发Page_Load - ASP.NET dynamically adding UserControl to PlaceHolder, not fire Click Event, only Page_Load 如何防止两次Page_Load运行-Page.IsPostBack始终为false - how Prevent Twice Page_Load Running - Page.IsPostBack Is always false 使用Response.Redirect()时不会触发Page_Load事件 - Page_Load event not firing when using Response.Redirect()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM