Professional Documents
Culture Documents
9.4 9.5. Creating User Controls
9.4 9.5. Creating User Controls
<%@
<%@ 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#" %>
%>
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" %>
%>
<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