ASP.NET ascx vs. aspx – Do you reuse user controls

asp.netuser-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?

Best Answer

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.