ASPX vs ASCX (ASP.NET Page vs User Control)

A lot of web developers while learning ASP.NET get confused with the difference between an ASP.NET Page and ASP.NET User Control.

A User Control is a group of one or more ASP.NET Server Controls or HTML Controls that can be reused. So for example if you need to create a Login Control, you would group a bunch of TextBoxes, Label and Button Controls and create a User Control out of it. This User Control can then be reused on multiple pages, providing a Modular approach to web development.

User Control are similar to ASP.NET Web Pages (WebForm). User Control can have markup, code and script, just like Web Pages have. Since both the User Control and ASP.NET Web Page inherit from the TemplateControl class, they also share common methods and events.

However there are some basic differences between the two:


ASP.NET User Control

ASP.NET Page uses the extension .aspx For eg: Default.aspxUser Control uses the extension .ascx For eg: WebUserControl.ascx
ASP.NET Page begin with a Page Directive. For eg:
<%@ Page Language="C#"
Inherits="_Default" %>

User Control begin with a Control Directive. For eg:
<%@ Control Language="C#"
Inherits="WebUserControl" %>

ASP.NET Page can be viewed directly in the Browser.User Control cannot be viewed directly in the browser. User Controls are added to WebPages and you view them by requesting a web page in your browser.
ASP.NET Page has a HTML, Body and Form ElementUser Control does not have a HTML, Body or Form element. It is hosted inside an ASP.NET Page.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.


Jijo Venginikkadan said...

Good Explanation....

Unknown said...

A master page can contain zero or more master pages (nested master pages). However, it must be associated with one or more web pages (aspx) to be useful.

A web page can stand on its own. It does not need any master pages or user controls. It can also be associated with a master page and contain many user controls. It can also be viewed directly in a browser. A web page can dynamically load user controls at run time.

A user control (ascx) can exist on its own but can only be viewed by browsers when it is contained in a web page. A user control can contain zero or more user controls (nested controls).

So where does this leave us? Should i have a few web pages that load controls dynamically? Why not use the treeview as nav that has the control name as the url. When it postback it merely loads the control in the web page. Is it right or wrong... if it works does it matter!?! Alot of architecture is subjective and based on the architects frame of reference. The key things i find are, is it simple, can others understand it, does it scale, and does it make sense.