ASP .NET:动态加载控件 vs Visible = true

ASP .NET: Dynamic load controls vs Visible = true(ASP .NET:动态加载控件 vs Visible = true)

本文介绍了ASP .NET:动态加载控件 vs Visible = true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

推荐哪一个?让我解释一下我想要实现的目标!
我有一个页面被许多用户使用,每个用户都有不同的角色,比如管理员、操作员、普通用户.
当客户打开该页面时,我想显示一组控件(按钮),这取决于他们的角色.
admin 可以做 x 和 y,但是普通用户是不能做这些操作的.

Which one is recommended ? Let me explain what I want to achieve !
I have one pages used by many users, every user has a different Role like admin, operator, normal user.
When a client open that page I want to display a set of controls (buttons) which depends on their Role.
admin is allowed to do x and y, but a normal user is not allowed to do these actions.

为了实现我想做的事情,哪种方法最好?
我应该在 HTML 中定义所有控件然后切换 Visible 属性,还是动态加载所需的控件?

In order to achieve what I want to do, which approach is the best one ?
Should I define all controls in HTML then toggle Visible property, or dynamically load needed controls ?

对于 Visible = false,我担心服务器处理时间.即使没有将 HTML 标记发送给 Visible = false 控件的客户端,我知道该控件仍由 ASP .NET 加载,甚至可能进行处理,但他的 HTML 结果并未写入输出流.

For Visible = false I'm worried about server processing time. Even if HTML markup is not sent to the client for a Visible = false control, I know that the control is still loaded by ASP .NET and maybe even processed, but his HTML result is not written to the output stream.

对于动态加载的控件,一个不方便的地方是它们需要在 Postback 上重新初始化,而且事件和 postback 也存在一些问题.

For dynamically loaded control, one inconvenient is that they need to be reinitialized on Postback, also there are some problems with events and postback.

推荐答案

我不会动态地这样做,因为收益不值得复杂性或感知到的节省.此外,如果您设置 visible = false,请记住您的控件仍启用视图状态.如果您担心来回数据并处理更大的视图状态,请确保禁用所有控件或包含它们的父面板的视图状态.但是,在回发时维护它们的状态与动态地维护它们的状态相同.

I wouldn't do it dynamically as the gain is not worth the complexity or perceived savings. Also if you set visible = false, keep in mind that the viewstate is still enabled for your controls. If you are worried about the back and forth data and dealing with a larger viewstate, make sure you disable the viewstate for all the controls or for a parent panel that contains them. You will have the same inconvenience for maintaining their state on postback as doing it dynamically though.

此外,对于下一个使用代码的人来说,以非动态方式进行操作更容易维护.与试图弄清楚什么时候把什么代码放在哪里相比,布局很明显并且更容易可视化.

Also, doing it non-dynamically is much easier to maintain to the next guy working with the code. The layout is obvious and easier to visualize than trying to figure out what code when is putting what where.

除了排除视图状态和可能可以忽略不计的处理服务器端之外,动态创建控件实际上并没有给您带来太多好处.我认为您会发现甚至很难衡量很多明显的差异,即使在非视图状态控件和根据需要动态添加它们的开销之间也是如此.

Creating controls dynamically really doesn't gain you much at all except for the exclusion of viewstate and maybe negligable processing server side. I think you would find it difficult to even measure much of a noticable difference, even under load between, a non-viewstate control and the overhead of dynamically having to add them as needed.

最后,不动态地做它更容易,所以为什么不先采取最简单的路线,看看它是否有问题.如果确实成为问题,则在必要时对其进行改进.

Lastly, it's easier to not do it dynamically so why not take the easiest route first and see if it is a problem. If it does become an issue then refine it where necessary.

这篇关于ASP .NET:动态加载控件 vs Visible = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ASP .NET:动态加载控件 vs Visible = true