Web Design that taps into the haromny and vision of your dreams.

Difference between ASP.Net User and Custom Server Controls

Comparison of ASP.Net User Controls and Custom Server Controls

Written By on in C#

397 words, estimated reading time 2 minutes.

User controls and Server controls are two methods for reusing common code or user interface components. There are several big differences between the two types of control, both in how they are created and how they are used.

Active Server Pages (ASP.Net) Series
  1. What is ASP.Net?
  2. ASP.Net for PHP Developers
  3. Creating a Simple ASP.Net Page
  4. ASP.Net Website Navigation Using a SiteMap
  5. Using ASP.Net Master Pages and Content Pages
  6. Validating Input using ASP.Net
  7. Tracing and Remote Debug in ASP.Net
  8. Creating Custom Error Pages with ASP.Net
  9. Managing Session State in ASP.Net
  10. Using Themes and Skins in ASP.Net
  11. Creating User Controls in ASP.Net
  12. Difference between ASP.Net User and Custom Server Controls
  13. Creating Fully Themable Websites with ASP.Net
  14. Ultimate Guide to the Web.Config File
  15. Adding StyleSheets to ASP.Net via C#
  16. Extending the Web Sitemap Xml Document

Let's go over the main differences first.

User Controls

  • Have an ascx extension.
  • Are compiled at runtime when the page is loaded.
  • Visual design is possible, just like an aspx page and uses the ASP.Net page model with code behind file.
  • Can only be used on a host aspx page or another user control.
  • Cannot be added to the ToolBox.
  • Can only be used in the current web application (source must be copied to another application to use).

Custom Server Control

  • Exist in precompiled assemblies.
  • Code entirely contained in .cs (or .vb)
  • No visual designer. Any HTML code needs to be declared programmatically.
  • Can be used in .aspx pages, user controls or other custom server controls.
  • Can be added to the ToolBox (drag and drop)
  • Can be shared between web applications.

A Custom Server Control is precompiled, which gives it component-like behaviour and allows the control to be installed into the global assembly cache. If designed correctly it will look and feel like an ASP.Net built-in server control such as Text Box or Label control.

A User Control is compiled at run time and exists in an ascx file in the same way a page exists in an aspx file. When the compiler finds the Register directive in an .aspx page that point to a user control, it compiles the control before it inserts its content into the .aspx page.

From a design point of view, User Controls can be designed in the visual editor in the same way as a page, and the code exists in a code behind file. Server Controls, on the other hand, exist in pure cs files with no visual interface. Any visual HTML code/CSS must be hand coded and written out in the cs code behind.

Which One?

User controls are best for reusable user interface components that are specific to a single application, or when visual design/ease of design are an issue. User controls cannot be shared between applications unless the code is replicated (not recommended as it creates duplicate code and maintenance issues).

Custom Server Controls should be used whenever a component or control should be shared between multiple applications. Because the server control exists in an assembly it can be strongly named and installed into the GAC on the web server.

For more details on creating User Controls please see our user control tutorials.

Last updated on: Thursday 22nd June 2017



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me