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

Developing Web Applications

Using
Microsoft® Visual
Studio® 2008
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?


What Is a User Control?
• Consists of a user-defined Web server control with
an .ascx extension
• Simplifies the reuse of code and UI components in a Web
application
• Contains HTML, but not the <HTML>, <BODY>, or
<FORM> tags
<%@ Control Language="C#" %>

or
<%@ Control Language="VB" %>

• 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
Lesson: Creating User Controls
• Creating a User Control

• Adding a User Control to a Web Form

• Demonstration: Creating and Implementing a User Control


Creating a User Control
• Choose a method for user control creation:
 Create a new user control by using Visual Studio 2008
 Convert an existing ASP.NET page to a user control

• Create properties in the user control to enable the


control to interact with the host page
 Public variables, properties, and methods of a user control
become the properties and methods of the control in the
host page
Adding a User Control to a Web Form
• Use the @ Register directive to include a user control in
an ASP.NET Page
<%@ Register src="WebUserControl.ascx"
tagname="WebUserControl" tagprefix="uc1" %>

• Insert the user control in a Web Form


<uc1:WebUserControl ID="WebUserControl1"
runat="server" />

• Use Get and Set properties of the user control


[Visual C#]
num1.pNum = 5; 'uses Set
x = num1.pNum; 'uses Get

[Visual Basic]
num1.pNum = 5 'uses Set
x = num1.pNum 'uses Get
Demonstration: Creating and Implementing a
User Control
• Create a new user control

• Create a property

• Insert a user control on a page

• Implement the functionality of the user control in the


code-behind page
Lab: Creating and Implementing User Controls
• Exercise 1: Creating a User Control

• Exercise 2: Implementing a User Control

Logon information
Virtual machine 2310C-LON-DEV-07

User name Student


Password Pa$$w0rd

Estimated time: 30 minutes


Lab Scenario

Master Page
Logon Page benefitsMaster.master
login.aspx
Benefits Lab Web
Home Page Application
ASPState
Default.aspx Page Header
header.ascx
Menu Component
Registration Benefits.cs or Benefits.vb
register.aspx TempDB
Web.
config

Life Insurance Retirement Medical Dentists


life.aspx retirement.aspx medical.aspx dental.aspx

Prospectus
prospectus.aspx Doctors User Control XML Web
LINQ to SQL doctors.aspx nameDate.ascx Service
Classes DentalService1.asmx
Doctors.dbml

Doctors Dentists
XML Files
Lab Review
Review Questions
• What is the purpose of the properties that you created for
the user control in Exercise 1?
• What is the syntax of the @ Register directive for the
user control?
• What is the HTML tag syntax for the user control?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Best Practices

You might also like