Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET ascx vs. aspx - Do you reuse user controls?

Our team is developing a rather big ASP.NET web project which initially started in ASP.NET 1.0 and was ported several times to all new versions of .NET.

We made extensively use of User Controls (ascx). But in retrospect I doubt that it was a good decision. A very small percentage of these controls are reused (resuable) through different pages. There is only this layer of complexity added to the application making some things more complex.

For reusable, small and specialized controls we use Server Controls (inherited from the WebControl class), which work great.

So my question: starting a new project, is it okay to get rid of ascx and implement all in the pages themselves (aspx)? Maybe except if you do a lot of dynamical loading (we do not) of user controls? What is your experience and what would you advice?

like image 803
splattne Avatar asked Nov 12 '08 09:11

splattne


2 Answers

We have some projects which use ASCX controls extensively, and others that don't. In my experience you have to decide on a case by case basis.

My two favourite reasons for using ASCX controls are:

  1. You're implementing a piece of UI functionality which will appear on many different pages (or multiple times on the same page).
  2. You want to encapsulate some complex functionality to keep it distinct from the rest of the page, thus making your code easier to read and more maintainable.

ASCX controls can be useful, but you really need to make sure you only use them when there's a good reason to - otherwise - as you say, you can be adding unnecessary complexity into your codebase.

like image 123
Chris Roberts Avatar answered Nov 02 '22 03:11

Chris Roberts


Used correctly, user controls improve maintenance and reusability, and very often are faster to create than server controls.

You mentioned that in your previous project, very few of the user controls were reused. This seems to be more related to poor planning than an inherent flaw in user controls themselves.

I guess a good rule of thumb would be to look at your design and see if you're using the same set of controls over and over again, in which case it does make sense to use user controls.

like image 30
Maxam Avatar answered Nov 02 '22 04:11

Maxam