gpt4 book ai didi

asp.net - 面板和用户控件与动态加载用户控件

转载 作者:行者123 更新时间:2023-12-02 11:19:35 25 4
gpt4 key购买 nike

如果我想动态显示或隐藏多个用户控件。使用加载用户控件然后显示或隐藏面板(可见=真/假)的面板会更好,还是使用占位符并在运行时加载(添加)或卸载(清除)它们会更好(LoadControl ())?

a) 我有一个使用 AJAX UpdatePanel 的页面。该页面有10个Panel,每个Panel加载一个用户控件。在后面的代码中,我使面板可见或不可见,一次只显示一个。这个解决方案很简单,因为所有用户控件都在页面中,很容易引用它们及其内部控件。

b) 使用 LoadControl 动态加载控件似乎有点复杂。

我想知道与方法a相比有什么好处吗? View 状态问题?渲染性能问题? HTML 大小问题?

谢谢,

一个

最佳答案

我更喜欢选项 A。动态加载控件并在回发中重新创建它们总是比在 aspx-markup 中切换控件的可见性更加复杂和容易出错。

我必须承认,我不明白您为什么要区分面板和占位符。主要区别仅在于 Panel 呈现为 DIV,而 PlaceHolder 根本没有 html。

You should lazy-load your UserControls if you have them all in page but only single UserControls visible. That means:

Do nothing in the UserControl's Page_Load but only when the controller(page) makes it visible. Therefore expose functions to load their data and update their inner UpdatePanels in the ascx. Then - after the function was called from the page and the control was made visible - let the page update the outer UpdatePanel around the UserControl.

通过这种方式,用户控件在可见之前既不会被渲染,也不会被数据绑定(bind),并且您始终可以直接直接引用它们。

Let them communicate in the following ways:

  • 页面 > 通过函数/属性进行用户控制
  • 用户控件 > 通过事件进行页面

Conclusion:

  • 选项 B 在 ViewState 和 HTML-Size 方面都没有优势。仅当您确实需要时才动态创建控件(动态内容)!

关于asp.net - 面板和用户控件与动态加载用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5587654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com