SlideShare a Scribd company logo
1 of 13
   A User Control is a file you create that contains a set
    of other ASP.NET controls and code grouped
    together to provide common functionality.

   The user control can then be used on different
    pages within a Web site.

   User controls in ASP.NET are created as .ascx files.
    An .ascx file is similar to the Web page’s .aspx file
    and can have its own code-behind page.

   The user controls you create inherit from the
    UserControl class.
   User controls are created using a procedure similar to
    building a standard Web page.

   Add New Item -> Web User Control.

   This adds a file with the .ascx extension to your
    application.

   The user control has both a Design and Source view
    similar to that of an .aspx page.

   However, a quick glance at the markup reveals the
    @ Control directive instead of the @ Page directive.
   User controls can have their own encapsulated
    events. This includes life cycle events such as Init
    and Load.

   User controls can also cause PostBack for the Web
    page to which they belong.

   The event handlers for a user control, however, are
    typically encapsulated in the given user control.

   This ensures the code for these events can be
    maintained independent of the pages that use the
    user controls.
public class AddressEventArgs : EventArgs
{
public AddressEventArgs(string addressLine1, string addressLine2,
string city, string state, string zip)
{
this.AddressLine1 = addressLine1;
this.AddressLine2 = addressLine2;
this.City = city;
this.State = state;
this.Zip = zip;
}
public string AddressLine1 { get; private set; }
public string AddressLine2 { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public string Zip { get; private set; }
}
   Next, you should declare a delegate.

    The delegate can be put in the same class file
    that contains both the event arguments and the
    user control class.

    public delegate void SaveButtonClickHandler(object
    sender, AddressEventArgs e);


   The next step is to add code to the user control
    that defines an event and raises that event
    when the user clicks the button.
public event SaveButtonClickHandler SaveButtonClick;

protected void ButtonSave_Click(object sender, EventArgs e)
{
if (SaveButtonClick != null)
{
SaveButtonClick(this, new
    AddressEventArgs(TextBoxAddress1.Text,
TextBoxAddress2.Text, TextBoxCity.Text, TextBoxState.Text,
TextBoxZip.Text));
}
}
   Finally, you add code to the page that contains the user
    control. This code should trap the event exposed by the
    user control.
   In C#, you wire up a handler using the += syntax inside the
    Page_Init method. Inside your handler, you use
    AddressEventArgs as required.

    protected void Page_Init(object sender, EventArgs e)
    AddressUc1.SaveButtonClick += this.AddressUc_SaveButtonClick;
    }
    private void AddressUc_SaveButtonClick( object sender,
    AddressEventArgs e)
    {
    UserProfile.AddNewAddress(this._userId, AddressUc1.AddressType,
    e.AddressLine1, e.AddressLine2, e.City, e.State, e.Zip);
    }
   Properties added to user controls can then be
    configured in the page markup that uses the
    control.

   When developers use the user control, they can set
    the properties declaratively through markup. In
    fact, these properties are also available through
    IntelliSense.

    public int UserId { get; set; }
    public UserProfile.AddressType AddressType {
    get; set; }
   Imagine the Address user control needs to allow
    users to preset values of the controls contained by
    the user control.
   In this case, you can expose the TextBox.Text
    properties as properties of the user control.
    public string AddressLine1
    {   get
        {
        return TextBoxAddress1.Text;
        }
        set
        {
        TextBoxAddress1.Text = value;
        }
    }
<%@ Page Language=”VB” AutoEventWireup=”false”
CodeFile=”UserProfilePage.aspx.vb” Inherits=”UserProfilePage” %>
<%@ Register src=”AddressUc.ascx” tagname=”AddressUc”
tagprefix=”uc1” %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>User Profile Settings</title>
</head>
<body style=”font-family: Verdana; font-size: small”>
<form id=”form1” runat=”server”>
<div>
<uc1:AddressUc ID=”AddressUc1” runat=”server”
AddressType=”Home” />
</div>
</form>
</body>
</html>
   To dynamically load a user control, you use
    the LoadControl method of the Page class.
   This method takes the name and path to a file
    that contains the user control’s definition.
   The method also returns a reference to the
    control class it creates.
    AddressUc addressControl =
    (AddressUc)LoadControl(“AddressUc.ascx”);
    form1.Controls.Add(addressControl);
   A templated custom Web control provides
    separation of control data from its presentation.

   This means that a templated control does not
    provide a default UI.

   Instead, this layout is provided by the developer
    who uses the user control on his or her page.

   This provides increased flexibility in terms of layout
    while keeping the encapsulation and reuse benefits
    of a user control.

More Related Content

What's hot (20)

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Dtd
DtdDtd
Dtd
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Html frames
Html framesHtml frames
Html frames
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xhtml
XhtmlXhtml
Xhtml
 
Struts
StrutsStruts
Struts
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
Struts framework
Struts frameworkStruts framework
Struts framework
 
Event handling
Event handlingEvent handling
Event handling
 

Viewers also liked

Introducing asp
Introducing aspIntroducing asp
Introducing aspaspnet123
 
Deploying configuring caching
Deploying configuring cachingDeploying configuring caching
Deploying configuring cachingaspnet123
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 
Mobile application
Mobile applicationMobile application
Mobile applicationaspnet123
 
Globalization and accessibility
Globalization and accessibilityGlobalization and accessibility
Globalization and accessibilityaspnet123
 
Custom controls
Custom controlsCustom controls
Custom controlsaspnet123
 
Monitoring, troubleshooting,
Monitoring, troubleshooting,Monitoring, troubleshooting,
Monitoring, troubleshooting,aspnet123
 
ความรู้เกี่ยวกับอินเทอร์เน็ตบี
ความรู้เกี่ยวกับอินเทอร์เน็ตบีความรู้เกี่ยวกับอินเทอร์เน็ตบี
ความรู้เกี่ยวกับอินเทอร์เน็ตบีPheeranan Thetkham
 

Viewers also liked (9)

Introducing asp
Introducing aspIntroducing asp
Introducing asp
 
Deploying configuring caching
Deploying configuring cachingDeploying configuring caching
Deploying configuring caching
 
Profile
ProfileProfile
Profile
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 
Mobile application
Mobile applicationMobile application
Mobile application
 
Globalization and accessibility
Globalization and accessibilityGlobalization and accessibility
Globalization and accessibility
 
Custom controls
Custom controlsCustom controls
Custom controls
 
Monitoring, troubleshooting,
Monitoring, troubleshooting,Monitoring, troubleshooting,
Monitoring, troubleshooting,
 
ความรู้เกี่ยวกับอินเทอร์เน็ตบี
ความรู้เกี่ยวกับอินเทอร์เน็ตบีความรู้เกี่ยวกับอินเทอร์เน็ตบี
ความรู้เกี่ยวกับอินเทอร์เน็ตบี
 

Similar to User controls

Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXIMC Institute
 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component PatternsMatthew Beale
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfoliomwillmer
 
Source Code Analysis with SAST
Source Code Analysis with SASTSource Code Analysis with SAST
Source Code Analysis with SASTBlueinfy Solutions
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state managementpriya Nithya
 
How to develop asp web applications
How to develop asp web applicationsHow to develop asp web applications
How to develop asp web applicationsDeepankar Pathak
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGProf Ansari
 
Introduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORKIntroduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORKKarthik Subramanian
 
Introduction to the integral framework
Introduction to the integral frameworkIntroduction to the integral framework
Introduction to the integral frameworkKarthik Subramanian
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsAleksandar Ilić
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsPSTechSerbia
 
State management in asp
State management in aspState management in asp
State management in aspIbrahim MH
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kitVidhi Patel
 

Similar to User controls (20)

Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
 
Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAX
 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component Patterns
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
Source Code Analysis with SAST
Source Code Analysis with SASTSource Code Analysis with SAST
Source Code Analysis with SAST
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Visual studio 2008 asp net
Visual studio 2008 asp netVisual studio 2008 asp net
Visual studio 2008 asp net
 
2310 b 05
2310 b 052310 b 05
2310 b 05
 
Java script
Java scriptJava script
Java script
 
How to develop asp web applications
How to develop asp web applicationsHow to develop asp web applications
How to develop asp web applications
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMING
 
Introduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORKIntroduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORK
 
Introduction to the integral framework
Introduction to the integral frameworkIntroduction to the integral framework
Introduction to the integral framework
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
State management in asp
State management in aspState management in asp
State management in asp
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
 
Managing states
Managing statesManaging states
Managing states
 
Asp.net
Asp.netAsp.net
Asp.net
 

Recently uploaded

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Recently uploaded (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

User controls

  • 1.
  • 2. A User Control is a file you create that contains a set of other ASP.NET controls and code grouped together to provide common functionality.  The user control can then be used on different pages within a Web site.  User controls in ASP.NET are created as .ascx files. An .ascx file is similar to the Web page’s .aspx file and can have its own code-behind page.  The user controls you create inherit from the UserControl class.
  • 3. User controls are created using a procedure similar to building a standard Web page.  Add New Item -> Web User Control.  This adds a file with the .ascx extension to your application.  The user control has both a Design and Source view similar to that of an .aspx page.  However, a quick glance at the markup reveals the @ Control directive instead of the @ Page directive.
  • 4. User controls can have their own encapsulated events. This includes life cycle events such as Init and Load.  User controls can also cause PostBack for the Web page to which they belong.  The event handlers for a user control, however, are typically encapsulated in the given user control.  This ensures the code for these events can be maintained independent of the pages that use the user controls.
  • 5. public class AddressEventArgs : EventArgs { public AddressEventArgs(string addressLine1, string addressLine2, string city, string state, string zip) { this.AddressLine1 = addressLine1; this.AddressLine2 = addressLine2; this.City = city; this.State = state; this.Zip = zip; } public string AddressLine1 { get; private set; } public string AddressLine2 { get; private set; } public string City { get; private set; } public string State { get; private set; } public string Zip { get; private set; } }
  • 6. Next, you should declare a delegate.  The delegate can be put in the same class file that contains both the event arguments and the user control class. public delegate void SaveButtonClickHandler(object sender, AddressEventArgs e);  The next step is to add code to the user control that defines an event and raises that event when the user clicks the button.
  • 7. public event SaveButtonClickHandler SaveButtonClick; protected void ButtonSave_Click(object sender, EventArgs e) { if (SaveButtonClick != null) { SaveButtonClick(this, new AddressEventArgs(TextBoxAddress1.Text, TextBoxAddress2.Text, TextBoxCity.Text, TextBoxState.Text, TextBoxZip.Text)); } }
  • 8. Finally, you add code to the page that contains the user control. This code should trap the event exposed by the user control.  In C#, you wire up a handler using the += syntax inside the Page_Init method. Inside your handler, you use AddressEventArgs as required. protected void Page_Init(object sender, EventArgs e) AddressUc1.SaveButtonClick += this.AddressUc_SaveButtonClick; } private void AddressUc_SaveButtonClick( object sender, AddressEventArgs e) { UserProfile.AddNewAddress(this._userId, AddressUc1.AddressType, e.AddressLine1, e.AddressLine2, e.City, e.State, e.Zip); }
  • 9. Properties added to user controls can then be configured in the page markup that uses the control.  When developers use the user control, they can set the properties declaratively through markup. In fact, these properties are also available through IntelliSense. public int UserId { get; set; } public UserProfile.AddressType AddressType { get; set; }
  • 10. Imagine the Address user control needs to allow users to preset values of the controls contained by the user control.  In this case, you can expose the TextBox.Text properties as properties of the user control. public string AddressLine1 { get { return TextBoxAddress1.Text; } set { TextBoxAddress1.Text = value; } }
  • 11. <%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”UserProfilePage.aspx.vb” Inherits=”UserProfilePage” %> <%@ Register src=”AddressUc.ascx” tagname=”AddressUc” tagprefix=”uc1” %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head runat=”server”> <title>User Profile Settings</title> </head> <body style=”font-family: Verdana; font-size: small”> <form id=”form1” runat=”server”> <div> <uc1:AddressUc ID=”AddressUc1” runat=”server” AddressType=”Home” /> </div> </form> </body> </html>
  • 12. To dynamically load a user control, you use the LoadControl method of the Page class.  This method takes the name and path to a file that contains the user control’s definition.  The method also returns a reference to the control class it creates. AddressUc addressControl = (AddressUc)LoadControl(“AddressUc.ascx”); form1.Controls.Add(addressControl);
  • 13. A templated custom Web control provides separation of control data from its presentation.  This means that a templated control does not provide a default UI.  Instead, this layout is provided by the developer who uses the user control on his or her page.  This provides increased flexibility in terms of layout while keeping the encapsulation and reuse benefits of a user control.