21 December 2010

What are the basic differences between user controls and custom controls?

What are the basic differences between user controls and custom controls?

User control

Deployment

Designed for single-application scenarios ,Deployed in the source form (.ascx) along with the source code of the application

If the same control needs to be used in more than one application, it introduces redundancy and maintenance problems

Custom control

Designed so that it can be used by more than one application Deployed either in the application's Bin directory or in the global assembly cache

Distributed easily and without problems associated with redundancy and maintenance.

Creation

User control

Creation is similar to the way Web Forms pages are created; well-suited for rapid application development (RAD)

Custom control

Writing involves lots of code because there is no designer support.

Content

User control

A much better choice when you need static content within a fixed layout, for example, when you make headers and footers

Custom Control

More suited for when an application requires dynamic content to be displayed; can be reused across an application,
for example, for a data bound table control with dynamic rows

Design

User Control

Writing doesn't require much application designing because they are authored at design time and mostly contain static data

Custom control

Writing from scratch requires a good understanding of the control's life cycle and the order in which events execute,
which is normally taken care of in user controls


How a user control is processed

When a page with a user control is requested, the following occurs:
The page parser parses the .ascx file specified in the Src attribute in the @ Register directive and generates a class that derives from the System.Web.UI.UserControl class.

The parser then dynamically compiles the class into an assembly.
If you are using Visual Studio, then at design time only, Visual Studio creates a code behind file for the user control, and the file is precompiled by the designer itself.

Finally, the class for the user control, which is generated through the process of dynamic code generation and compilation, includes the code for the code behind file (.ascx.cs) as well as the code written inside the .ascx file.


How to choose the base class for your custom control

To write a custom control, you should directly or indirectly derive the new class from the System.Web.UI.Control class or from the System.Web.UI.WebControls.WebControl class:
You should derive from System.Web.UI.Control if you want the control to render nonvisual elements. For example, and are examples of nonvisual rendering.

You should derive from System.Web.UI.WebControls.WebControl if you want the control to render HTML that generates a visual interface on the client computer.

No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...