Difference between ASP.Net User and Custom Server Controls
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.
- What is ASP.Net?
- ASP.Net for PHP Developers
- Creating a Simple ASP.Net Page
- ASP.Net Website Navigation Using a SiteMap
- Using ASP.Net Master Pages and Content Pages
- Validating Input using ASP.Net
- Tracing and Remote Debug in ASP.Net
- Creating Custom Error Pages with ASP.Net
- Managing Session State in ASP.Net
- Using Themes and Skins in ASP.Net
- Creating User Controls in ASP.Net
- Difference between ASP.Net User and Custom Server Controls
- Creating Fully Themable Websites with ASP.Net
- Ultimate Guide to the Web.Config File
- Adding StyleSheets to ASP.Net via C#
- Extending the Web Sitemap Xml Document
Adverts Blocked Please disable AdBlocking software and allow me to set cookies so that I can continue providing free content and services.
Let's go over the main differences first.
- 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.
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!