Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Module 8

Creating User Controls


Overview

• Adding User Controls to an ASP.NET Web Form


• Creating User Controls
• In addition to HTML and Web server controls,
you can easily create your own controls that
can be reused across Web applications
• These controls are called user controls
• User controls provide an easy way to reuse
common user interface (UI) components and
code across a Web application
• After completing this module, you will be able
to:
– Add a user control to a MS ASP.NET Web Form
– Create a user control
Lesson: Adding User Controls to an
ASP.NET Web Form
• What is a User Control?
• Why Use User Controls?
• Adding a User Control
• A user control is an ASP.NET page that can be imported
as a server control by other ASP.NET Web Forms
• Similar to Web server controls, which are components
that run on the server, user controls provide UI and
other related functionality
• After you have created a user control, it can then be
used by other pages in the same Web application
• In this lesson, you’ll learn what user controls are, why
you should consider using them in you Web
applications, how to reference a user control from an
ASP.NET Web Form and then learn how to access the
properties in a user control.
• After completing this lesson, you’ll be able to:
– Explain what a user control is and why you might use
one
– Add a user control in an ASP.NET page
– Access user control properties in an ASP.NET page
What is a User Control?
• User controls simplify the reuse of code and UI components within a
Web application
• A user control is a user-defined Web server control with an .ascx
extension
• Contains HTML, but not the <HTML>, <BODY>, or <FORM> tags

<%@
<%@ Control
Control Language="VB"
Language="VB" AutoEventWireup="false"
AutoEventWireup="false"
CodeFile="WebUserControl.ascx.vb"
CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl"
Inherits="WebUserControl" %>
%>
or

<%@
<%@ Control
Control Language="c#"
Language="c#" %>
%>

• Contains code to handle its own events (.ascx.vb or .ascx.cs )


• Definition
– User controls are ASP.NET pages with an .ascx file
extension
– User controls offer you an easy way to partition and reuse
common UI functionality across your ASP.NET Web
applications
– Similar a Web Forms pages, you can author these controls
with any text editor or develop them by using code-behind
classes
– Also, similar to a Web Forms page, user controls are
compiled when first requested and then stored in server
memory to reduce the response time for subsequent
requests
– Unlike Web Form pages, however, user controls cannot be
requested independently; user controls must be included
in a Web Forms page to work
Why Use User Controls?

• Reuse user interface and code

Control1.ascx Application A Application B

Page3.aspx
Page1.aspx

Page2.aspx
Why Use User Controls?
User controls offers many advantages when developing a Web
application:
• User controls are self contained
User controls provides separate variable namespaces, which means
that none of the methods and properties of user control conflict with
any existing methods or properties of the hosting page.
• User controls can be used more than once within the hosting
page, without causing property and method conflicts.
• User controls can be written in a different language from the
main hosting page. Eg: A user control is written in C# and Web form is
written in VB.NET.
Why Use User Controls?
The examples where a user control simplifies Web page
development:
• Text box to only accept integers in a specific range. The
user control would include validation controls.
• Data grid control that always connects to the same data
source, to display data from a stored procedure or table.
•Grouping of text boxes and other controls to accept mailing
information ( name, address, state, city, postcode) form
user.
•Grouping of text boxes and other controls to accept contact
information (home telephone number, business telephone
number, mobile telephone, e-mail address) form user.
Adding a User Control
• Use the @ Register directive to include a user control in an
ASP.NET Page

<%@
<%@ Register
Register Src="WebUserControl.ascx"
Src="WebUserControl.ascx"
TagName="WebUserControl"
TagName="WebUserControl" TagPrefix="uc1"
TagPrefix="uc1" %>
%>

• Insert the user control in a Web Form

<uc1:WebUserControl
<uc1:WebUserControl ID="WebUserControl1"
ID="WebUserControl1"
runat="server"
runat="server" />
/>
Adding a User Control
• Use Get and Set properties of the user control to expose the
values of text box to the host.

num1.pNum
num1.pNum == 55 'uses
'uses Set
Set
xx == num1.pNum
num1.pNum 'uses
'uses Get
Get
or

num1.pNum
num1.pNum == 5;
5; //uses
//uses Set
Set
xx == num1.pNum;
num1.pNum; //uses
//uses Get
Get
Lesson: Creating User Controls
• Creating a User Control
• Demonstration: Using a User Control
Review

• Adding User Controls to an ASP.NET Web Form


• Creating User Controls
Exercise
• List TWO examples where a user control simplifies Web page
development.
• How do you access the properties of a user control’s UI elements
from the host page?
• How do you reference a user control from an ASP.NET Web form?
• Can you use two different user controls with the same name in the
same ASP.NET page?
• How can you use a user control in two different Web applications?
~ End of Slides ~

You might also like