You are on page 1of 15

Creating and Implementing

User Controls
Module 7: Creating and Implementing User
Controls
• Adding User Controls to an ASP.NET Web Form

• Creating User Controls


Lesson: Adding User Controls to an ASP.NET Web
Form
• What Is a User Control?

• Why Use User Controls?

• Adding a User Control

• Demonstration: Creating a User Control


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 Language="VB" %>

• or

<%@ Control Language=“C#" %>

• Contains code to handle its own events


Why Use User Controls?

• Reuse user interface and code

Control1.ascx Application A Application B

Page3.aspx
Page1.aspx

Page2.aspx
Adding a User Control
• Use the @ Register directive to include a user control in an
ASP.NET Page

<%@ Register TagPrefix="demo"


TagName="validNum" Src="numberbox.ascx" %>

• Insert the user control in a Web Form


<demo:validNum id="num1" runat="server"/>

• Use Get and Set properties of the user control

num1.pNum = 5 'uses Set


x = num1.pNum 'uses Get

• or
num1.pNum = 5; 'uses Set
x = num1.pNum; 'uses Get
Demonstration: Creating a User Control
• Create a new user control

• Create a property
Lesson: Creating User Controls
• Creating a User Control

• Demonstration: Implementing a User Control


Creating a User Control

• Two methods for user control creation:


 Create a new user control using Visual Studio 2008
 Convert an existing ASP.NET page to a user control

• Host page interacts with the control using properties


• Host page should use flow layout
Demonstration: Implementing a User Control
• Insert a user control on a page

• Use the user controls in the code-behind page


Creating and Implementing User Controls
• Exercise 1: Creating a User Control

• Exercise 2: Implementing a User Control

Logon information
Virtual machine 2310C_07
User name Student
Password Pa$$w0rd

Estimated time: xx minutes


Lab Scenario

Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config

Life Insurance Retirement Medical Dental


Life.aspx Retirement.aspx Medical.aspx Dental.aspx

Prospectus Doctors User Control XML Web


Lab Web Prospectus.aspx Doctors.aspx namedate.ascx Service
Application dentalService1.asmx

XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
• Review Questions

• What are two differences between a user control and a


component?
• How do you access the properties of a user control’s UI
elements from the host page?
• What are the four general steps in creating a user control?

• 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? Why or why not?
Review for Alpha
• Is there any topic or specific content item in the module
that seemed unclear or unnecessary?
• Is there any content item/related subject area that was
not covered and could be included?
• Did you observe any issues with the technical accuracy of
the content?
• Is the content in the module presented in a manner that
encourages learning? Did the flow of topics seem right?
• Does the lab outline indicate the expected scope of tasks
to be covered? Would you like to suggest any tasks that
could be removed or added?

You might also like