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 Page

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#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>

User Control begin with a Control Directive. For eg:
<%@ Control Language="C#"
AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
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.


Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

Jijo V.G said...

Good Explanation....

Steve 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.