Module 8: Creating User Controls

Download Report

Transcript Module 8: Creating User Controls

Module 8:
Creating User
Controls
Overview


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
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" AutoEventWireup="false"
CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl"
%>
or
<%@ Control Language="c#" %>

Contains code to handle its own events (.ascx.vb or .ascx.cs )
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, email address) form user.
Adding a User Control

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" />
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 = 5 'uses Set
x = num1.pNum 'uses Get
or
num1.pNum = 5; //uses Set
x = num1.pNum; //uses 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?
3 lab execises this week..
1: Data binding against object
(use list-bound controls)
2: Using master pages and site navigation
3: Creating and using User Control
List-Bound Controls

What is List-Bound Controls?
Def 1 :- The controls that can be
populated automatically with the data
from a data source
Def 2:- The controls that connect to a
data source and display the data.







Examples :
-CheckBoxList
-DataGrid
-DataList
-DropDownList
-ListBox
-RadioButtonList
- Repeater