Unit4 Ref
Unit4 Ref
NET
UNIT-4
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.
Microsoft Visual Studio .NET provides three types of custom control for use on
Web forms.
Custom control :
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.
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.
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.
Cannot be added to
Toolbox Can be added to toolbox
toolbox
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.
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.
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