tags:

views:

130

answers:

3

One of my friends has written a website that has so many user controls and pages.

The pages contain so many labels, hidden values and user controls. Based on user requests he is enabling and disabling those controls. In every webform pageload he is looping over Request.form.allkeys and comparing every control. Based on that comparison, he is enabling and disabling the controls on the webform.

I don't know whether it is a good programming practice or not.

Can anyone point me in the right direction?

+4  A: 

I inherited a website like this once. The person who wrote it must have been a masochist.

It is a horrible way to design a web site. The complexity of when to show/hide things, when to process input from which fields, is way too much. Maintenance becomes impossible because one tiny modification could have dire consequences.

A better approach would be to isolate common behavior into a single page. If that page is complicated, then take a core section of that page and make it into a user control. Decompose the UI into logical parts and piece them together on the page.

Your page code should be incredibly simple. The logic of hiding/showing specific fields is business logic and should be represented in code as expressively as possible.

Ben Scheirman
A: 

I'm not much of a webdev, but I know that would be considered very bad practice in the desktop app world, and I really doubt that it's much different in the web world.

Consider enabling and disabling only relevant controls when a particular action takes place, perhaps using a more MVC (or is it MVVM now?) approach. Your friend's application logic is very tightly coupled with the presentation logic, so if there's a need to change either, there will likely be ripple effects throughout the system.

Greg D
A: 

I've never heard of parsing form post values while using Web controls. When I did traditional ASP, that would have been my poison, but not with web controls.

As for wrapping tons of functionality into a single page, it is probably a bad idea. Having user controls makes sense for grouping blocks of related UI functionality, and even for hiding a section, but not so much for pulling in and out entire 'pages'. Just have different pages.

Also, if the reason for having so much functionality mixed in the same page is to use related code, then move related code to a library that can be used by all of the code-behinds. Going back to the user controls, use them for recurring UI elements, which can be used in any page.

Greg Ogle