ASP.NET: dynamic load management vs Visible = true

Which one is recommended? Let me explain what I want to achieve!
I have one page used by many users, each user has a different role, such as administrator, operator, regular user.
When the client opens this page, I want to display a set of controls (buttons) that depend on their role.
The administrator is allowed to do x and y, but the normal user is not allowed to perform these actions.

To achieve what I want to do, which approach is the best?
Should I define all the controls in HTML, then switch the Visible property or dynamically load the necessary controls?


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

For a dynamically loaded control it is inconvenient that they need to reinitialize Postback, there are also some problems with events and writeback.

+5
source share
4 answers

I would not do it dynamically, since winning is not worth the complex or perceived savings. Also, if you set visible = false , keep in mind that the view state is still enabled for your controls. If you are worried about back and fourth data and are dealing with a large view, be sure to turn off the viewstate for all controls or for the parent panel that contains them. You will have the same inconvenience to maintain your state during postback, as it happens dynamically.

Also, doing this non-dynamically is a lot easier to keep up with the next guy working with code. The layout is clear and easier to visualize than trying to figure out which code when puts where.

Creating controls dynamically does not really give you anything in common, except for an exception from the viewstate and possibly a sloppy processing server. I think it will be difficult for you to even measure most of the noticeable difference, even when loading between them, the non-viewstate control and the overhead associated with dynamically adding them as needed.

Finally, it’s easier not to do it dynamically, so why not take the simplest route and see if this is a problem. If this becomes a problem, check it where necessary.

+3
source

.Visible = false is a perfectly reasonable approach for this. Do not worry about the speed of this method until you prove through profiling that this is necessary.

+1
source

What to do if you install controls of different roles on different panels and just the entire visible / invisible panel

+1
source

Another disadvantage for dynamic controls is the large amount of fragile code that you have to write to process, and headaches when debugging them. If you don't have extremely complex controls that take a lot of time, or you really identified a performance issue, I would highly recommend an invisible method (and do it regularly). This is the KISS principle in action (not to mention the “do not optimize” principle beforehand).

+1
source

Source: https://habr.com/ru/post/886966/


All Articles