0% found this document useful (0 votes)
16 views5 pages

Unit4 Ref

The document discusses custom controls and user controls in ASP.NET. It covers creating user controls, the differences between custom controls and user controls, and the limitations of user controls.

Uploaded by

Niamrah Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Unit4 Ref

The document discusses custom controls and user controls in ASP.NET. It covers creating user controls, the differences between custom controls and user controls, and the limitations of user controls.

Uploaded by

Niamrah Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WEB APPLICATION USING C#.

NET
UNIT-4

SYLLABUS : UNIT - IV: CUSTOM CONTROLS

User Controls – Creating a Simple User Control – Visual Studio.NET Custom


Control Support – Independent User Controls – Integrated User Controls – User
Control Events – Limitations – Deriving Custom Controls.

4.1 INTRODUCTION :

ASP.NET provides the scope to develop and implement new user control,
modify existing controls through inheritance, and write a custom control that
does its own painting. Custom controls extend the tools available to Web
developers. Using custom controls, you can encapsulate key aspects of the
visual interface and program logic that you want to reuse throughout the
application.

 Their file extension is .ascx and not .aspx and it begins with “<%@
Control %>” directive instead of “<%@ Page %>“;
 The files containing their event handling logic (if used) inherit from the
System.Web.UI.UserControl base class. This base class provides the base
set of properties and methods you use to create the control.

4.2 TYPES OF CUSTOM CONTROLS

Microsoft Visual Studio .NET provides three types of custom control for use on
Web forms.

1. Web user controls


Web User Controls combine existing server and HTML controls by using the
Visual Studio .NET Designer to create functional units that encapsulate some
aspect of the user interface. User controls reside in content files, which must be
included in another project, in which the controls can be used.

2. Composite custom controls


These create new controls from existing server and HTML controls. Although
similar to user controls, composite controls are created in code rather than
visually, and therefore they can be compiled into an assembly (.dll), which can
be shared between multiple applications. The custom controls can be added in
the Toolbox in Visual Studio .NET and then can be used in another ASP.NET
application.
Web Application using C#.NET Unit 4 Jerald Page 1 / 5
3. Rendered custom controls
Rendered custom controls are created entirely with new controls by rendering
HTML directly rather than using composition. These controls are compiled
(DLL) and can be used from the Toolbox, just like composite controls. It is
necessary to write extra code to handle tasks that are performed automatically in
composite controls.

Custom control :

A custom control is a loosely coupled control defined in a class, which derives


from Control.

The UI of the custom control is generally defined in a


Resource Dictionary inside the resource file. They are created according to the
requirement of the business. To change the functionality of existing controls,
such as a button or label, you can directly derive the new class with these
existing classes and can change their default behaviour. We can create
themes for a custom control and reuse it in various projects very easily.

User control :

The base UserControl is nothing but a Custom Control that you derive to create
a control UI specific to your application. It provide the reusability of design and
are created in the same way as a web form. They have an .ascx extension.

User controls are quiet helpful if they have to be used only for a particular
website. They have a visual interface. These controls are loaded at the runtime,
and thus cannot be found in the toolbox. They offer an easy way to partition and
reuse common user interfaces across ASP.NET Web applications. They use the
same Web Forms programming model on which a Web Forms page works.

4.3 CustomControl Vs UserControl

The main difference between Custom Control and User Control is that they
inherit from different levels in the inheritance tree. A Custom Control
generally inherits from the System.Windows.Controls.Control class. It may be
derived from a different custom control depending on your requirement.

A UserControl inherits from the


System.Windows.Controls.UserControls class, which inherits from the base
"Control" class.

Web Application using C#.NET Unit 4 Jerald Page 2 / 5


Custom control is designed for single-application scenarios because if the same
control needs to be used in more than one application, it introduces redundancy
and maintenance problems. While UserControl designed so that it can be used
by more than one application. It can be distributed easily and without problems
associated with redundancy and maintenance .CustomControl is a loosely
coupled control with respect to code.

UserControl is a tightly coupled control w.r.t code and UI. When using
CustomControl UI can be changed in different projects but for a UserControl UI
is fixed and can't have different looks in different project. Moreover, creating a
custom control requires in-depth knowledge of Silverlight UI Model whereas
UserControl does not require in-depth knowledge of the UI Model.

User Control Custom Control


User controls are A custom control is the one
created just like a web which is made or created by
Definition form. They make use of the programmer to serve the
the existing controls to business needs, by extending
define their own logic. the functionality of existing
controls.
Creation Easy Complex

Runs on Do not run on their own


Run on their own dll
dll

Reusability Web page Control

Cannot be added to
Toolbox Can be added to toolbox
toolbox

Preferred for Static layout Dynamic layout

Single copy can be used Separate copy is required for


Usability
for different projects each application

Flexibility Less More

Web Application using C#.NET Unit 4 Jerald Page 3 / 5


4.4 STEPS FOR CREATING USER CONTROLS

STEP 1 : CREATING A CONTROL

1. Add a Web user control page (.ascx) to the project.


2. Draw the visual interface of the control in the designer.
3. Write code to create the control’s properties, methods, and events.
4. Use the control on a Web form by dragging it from Solution Explorer to
the Web form on which you want to include it.
5. Use the control from a Web form’s code by declaring the control at the
module level and then using the control’s methods, properties, and events as
needed within the Web form.

STEP 2 : To create properties and methods for the user control that can be used
in a Web form

1. Create the public property or method that you want to make available
on the containing Web form.
2. Write code to respond to events that occur for the controls contained
within the user control. These event procedures do the bulk of the work
for the user control.
3. If the property or method needs to retain a setting between page
displays, write code to save and restore settings from the control’s
ViewState.

4.5 TYPES OF USER CONTROLS

Independent user controls: They are the simpler type and they do not interact
with the other code on the form. The header control in this tutorial, so far, is an
example of an independent user control.

Integrated user controls: They are the more complex type and they interact
with the other page code. Usually this type of user control is implemented when
developers need to determine the behaviour of the control through some
properties.

Web Application using C#.NET Unit 4 Jerald Page 4 / 5


4.6 LIMITATIONS OF USER CONTROLS

As the user controls are not compiled into assemblies, they have the following
limitations:

1. A copy of the control must exist in each Web application project in which the
control is used.

2. User controls can’t be loaded in the Visual Studio .NET Toolbox; instead,
you must create them by dragging the control from Solution Explorer to the
Web form.(But Custom Controls can be added in ToolBox and can be used in
external applications)

3. User control code is initialized after the Web form loads, which means that
user control property values are not updated until after the Web form’s Load
event.

REFERENCES

1. https://livebook.manning.com/book/asp-dot-net-4-0-in-
practice/chapter-6/
2. http://net-informations.com/faq/asp/user.htm
3. https://www.tutorialspoint.com/asp.net/asp.net_custom_controls.htm
4. https://www.c-sharpcorner.com/uploadfile/sapnabeniwal/create-
custom-web-control-in-asp-net/
5. http://csresources.in/webapps/webapp_ex9.pdf
6. http://csresources.in/webapps/webapp_ex10.pdf
7. https://sites.google.com/site/dotnettips7/home/list-of-20-third-party-
controls-for-dotnet-applications

Web Application using C#.NET Unit 4 Jerald Page 5 / 5

You might also like