简体   繁体   English

在不应该呈现的控件上调用PageLoad和OnPreRender

[英]PageLoad and OnPreRender invoked on controls that should not be rendered

I'm using this oversimplified code block: 我正在使用这个过于简化的代码块:

<% if (MyCondition())
{ %>
<myUsedControl/>
<% }
else
{ %>
<myUnusedControl/>
<% } %>

in my ascx file. 在我的ascx文件中。 I assumed that when ASP.Net would read this page, if MyCondition() returned true, it would completely ignore whatever was in the else clause. 我假设当ASP.Net读取此页面时,如果MyCondition()返回true,它将完全忽略else子句中的内容。 This is not the case, the myUnusedControl's PageLoad and OnPreRendered events are still being fired when I load the page, even though myUnusedControl is properly hidden when the browser displays the page. 情况并非如此,即使在浏览器显示页面时myUnusedControl已正确隐藏,myUnusedControl的PageLoad和OnPreRendered事件仍会在我加载页面时触发。

Why is this? 为什么是这样? How can I make sure a chunk of ascx or aspx be completely ignored when a page is rendered? 如何确保呈现页面时完全忽略一部分ascx或aspx?

Thanks for your time. 谢谢你的时间。

ASP.NET cant deduce that MyCondition() does not depend on the execution of a subscribed PreRender event. ASP.NET无法推断MyCondition()不依赖于已订阅的PreRender事件的执行。 There's also the possibility that the method has side effects that shouldn't be executed twice, so it should only be called once, and as late as possible. 该方法还可能具有不应执行两次的副作用,因此应只调用一次,并应尽可能晚地调用。 There's also a requirement to keep all controls up-to-date in the event cycle; 还需要在事件周期中使所有控件保持最新状态。 how should the different components in your page work when one is not yet initialized, while others have already triggered their postback events? 当一个页面尚未初始化而其他页面已经触发了回发事件时,页面中的不同组件应该如何工作?

In a somewhat contrived example: 在一个有些人为的示例中:

Boolean _condition;
Boolean MyCondition() {
    return _condition;
}

void MyContrivedPreRender(Object sender, EventArgs e) {
    _condition = true;
}

<% if(MyCondition()) { %>
    <asp:Literal runat="server" Text="Hello world?"
                 OnPreRender="MyContrivedPreRender" />
<% } %>

You could always create a duplicate page with the 2nd control and put your if condition branching earlier in the pipeline to control which page gets loaded. 您总是可以使用第二个控件创建一个重复页面,并将if条件分支放在管道的前面,以控制要加载的页面。

For this example, you could always add the control manually to the controls collection in the code behind and do your branching around that rather than registering the control in the ascx/aspx page markup. 对于此示例,您始终可以将控件手动添加到后面的代码中的控件集合中,然后进行分支操作,而不是在ascx / aspx页面标记中注册控件。

Dynamically Load your control based on your conditions (LoadControl) in the Page OnInit and use that control variable in the methods you need. 根据页面OnInit中的条件(LoadControl)动态加载控件,并在所需的方法中使用该控件变量。

public class MyClass { MyUserControl _controlVariable ; 公共类MyClass {MyUserControl _controlVariable;

protected override void OnInit(EventArgs e)
{
     if (MyCondition())  
     { 
          _controlVariable  = Loadcontrol("control1.ascx");
     }

     else  
     { 
         _controlVariable  = Loadcontrol("control2.ascx");

     }
  }
}

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

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