简体   繁体   English

防止呈现用户控件

[英]Prevent rendering of user control

in our asp.net webforms application, we dynamically load usercontrols into placeholders. 在我们的asp.net Webforms应用程序中,我们将用户控件动态加载到占位符中。 In order to retain changes across post-backs, our page-life-cycle is a little more complex than usual. 为了保留回发之间的更改,我们的页面生命周期比平时要复杂一些。 We ALWAYS restore the previous control structure in pageInit in order to successfully load our viewstate. 我们总是在pageInit中还原以前的控件结构,以便成功加载我们的viewstate。 Only then do we clear the placeholder and load a new control into it. 只有这样,我们才清除占位符并将新控件加载到其中。

This unfortunately means an entire life-cycle both for the old AND the new usercontrol, including server-side-processing of the old module's entire .ascx markup-file. 不幸的是,这对旧用户控件和新用户控件都意味着整个生命周期,包括对旧模块的整个.ascx标记文件的服务器端处理。

Now my question: Is there any possibility to minimize server-side processing of the old module, as it never gets sent back to the client (ie it's server-side rendering is completely unnecessary). 现在我的问题是:是否有可能最小化旧模块的服务器端处理,因为它永远不会发送回客户端(即完全不需要服务器端渲染)。 What I'd ideally want to achieve is a sort of "light-weight" loading of a usercontrol, when it's only purpose is restoring vewstate information without it ever reaching the client. 我理想地希望实现的是一种“轻量级”的用户控件加载,当它的唯一目的是还原vewstate信息而不会到达客户端时。

The goal of the exercise is performance optimization. 该练习的目标是性能优化。

Any hints, ideas or suggestions appreciated! 任何提示,想法或建议表示赞赏!

I have resolved code running in webcontrol lifecycle events of dynamically added controls by simply checking if the control was visible ( http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx)- 通过简单地检查控件是否可见,已经解决了在动态添加控件的webcontrol生命周期事件中运行的代码( http://msdn.microsoft.com/zh-cn/library/system.web.ui.control.visible.aspx) -

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Visible) 
    {
       //Your code here
    }
}

If you have any methods that aren't triggered by a page lifecycle event, and have to be triggered by a user action, such as- 如果您有不是页面生命周期事件触发的任何方法,而必须由用户操作触发的方法,例如-

protected void Button1_Click(object sender, EventArgs e)
{
   //Do something
}

This can safely be left as is, the method code will not be run until the control is added to the page and the action is triggered. 可以放心地保持原样,直到将控件添加到页面并触发操作后,方法代码才会运行。

Although the visibility check doesn't feel especially elegant, it's probably the best way to deal with auto wired events on dynamically loaded controls. 尽管可见性检查并不特别优雅,但它可能是处理动态加载的控件上的自动关联事件的最佳方法。

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

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