SlideShare a Scribd company logo
   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

WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
rishisingh190
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Oomd unit1
Oomd unit1Oomd unit1
Oomd unit1
VivekChaudhary93
 
Oops in vb
Oops in vbOops in vb
Oops in vb
Dalwin INDIA
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
Nitesh Dubey
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
Object Oriented Modeling and Design with UML
Object Oriented Modeling and Design with UMLObject Oriented Modeling and Design with UML
Object Oriented Modeling and Design with UML
Malek Sumaiya
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
MustafaIbrahimy
 
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
Threads in JAVA
Threads in JAVAThreads in JAVA
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 

What's hot (20)

WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
Ajax and Jquery
Ajax and JqueryAjax and Jquery
Ajax and Jquery
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Oomd unit1
Oomd unit1Oomd unit1
Oomd unit1
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
 
Interface in java
Interface in javaInterface in java
Interface in java
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Object Oriented Modeling and Design with UML
Object Oriented Modeling and Design with UMLObject Oriented Modeling and Design with UML
Object Oriented Modeling and Design with UML
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Exception handling
Exception handlingException handling
Exception handling
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
javascript objects
javascript objectsjavascript objects
javascript objects
 

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

Chapter 11
Chapter 11Chapter 11
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
Ankit Gupta
 
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
IMC Institute
 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component Patterns
Matthew 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 SAST
Blueinfy Solutions
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya 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 PROGRAMMING
Prof Ansari
 
Introduction to the integral framework
Introduction to the integral frameworkIntroduction to the integral framework
Introduction to the integral framework
Karthik Subramanian
 
Introduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORKIntroduction to the INTEGRAL FRAMEWORK
Introduction to the INTEGRAL FRAMEWORKKarthik Subramanian
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
Rishi Kothari
 
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
Aleksandar 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 Components
PSTechSerbia
 
State management in asp
State management in aspState management in asp
State management in asp
Ibrahim MH
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kitVidhi Patel
 
Managing states
Managing statesManaging states
Managing states
Paneliya Prince
 

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
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
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
 

Recently uploaded

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 

Recently uploaded (20)

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 

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.