SlideShare ist ein Scribd-Unternehmen logo
1 von 16
28-08-2009                                 DirectI




             ASP.NET User Controls




                  Presenter: Viral Patel
ASP.NET Controls

    Types of Controls
      (1) HTML Server Controls (Traditional HTML Tags)
      (2) ASP.NET Server Controls (NEW ASP.NET Tags)
      (3) AJAX Server Controls (ASP.NET AJAX Controls)
      (4) Controls Created By Users
Controls created by Users

    ASP.NET User Control

    ASP.NET Custom Control
        • Derived Custom Control
        • Composite Custom Control
        • Fully Custom Control
ASP.NET User Controls
1. Prerequisites
2. User Control vs Web Page
3. Create ASP.NET User Control
4. Add User Control to an ASP.NET Web Page
5. Define Properties of ASP.NET User Control
6. Define Methods of ASP.NET User Control
1. Prerequisites

    ASP.NET Web Page
    
        Single Page Model
    
        Code-Behind Page Model
        −   Markup File - .aspx file (Design|Source)
        −   Code File - .aspx.(vb|cs) file
    
        ASPX file (Default.aspx)
        −   <%@ Page Language=”C#”
                 AutoEventWireUp=”true”
                 CodeFile=”Default.aspx.cs”
                 Inherits=”Default”
                 %>
Demo 1 : Create ASP.NET Web Page




           Note <%@ Page Directive
2.User Control vs Web Page

  ASP.NET Web Page               ASP.NET User Control

          ASPX                            ASCX

    <%@ Page … %>                  <%@ Control … %>

  System.Web.UI.Page             System.Web.UI.Control

can run as stand-alone file   cannot run as stand-alone file

    html, body, form          html, body, form – not present
3.Create ASP.NET User Control
•       ASP.NET User Control
    –     ASCX file (MyUserContol.ascx)
           •   <%@ Control Language=”C#”
                     AutoEventWireUp=”true”
                     CodeFile=”MyUserControl.ascx.cs”
                     Inherits=”MyUserControl”
                     %>
           •   MyUserControl inherits UserControl Class

•       Add ASP.NET Server Control(s)
•       Add ASP.NET Code for the Control(s)
Demo 2 : Create ASP.NET User Control
               User Control




         Note <%@ Control Directive


                 Web Page
4. Add User Control to an ASP.NET Web Page


    User Control cannot run stand alone

    It must be added to ASP.NET web page (aka host page) (.aspx)

    STEP 1 : User Control needs to be registered to the host page
        −   <%@ Register
                 TagPrefix=”UC”
                 TagName=”MyUserControl”
                 Src=”~/Controls/MyUserControl.ascx”
                 %>


    STEP 2 : add the user control to host page
        −   <TagPrefix:TagName ID=”UserControlID” runat=”server” />
        −   <UC:MyUserControl ID=”MyUserControl1” runat=”server” />


    ALTERNATIVE:
        −   Drag User Control and Drop to Design View of Web Page
Demo 3 : Register & Add User Control to Web Page

                      Registering User Control




Things to Note :    1. <%@ Register Directive
                    2. TagPrefix, TagName, Src - Attributes


                        Add User Control




                      Note User Control Declaration
5. Define Properties of ASP.NET User Control

    User Control can have properties like ASP.NET Web Controls.

    To add a property to a User Control
         −   Syntax:
             <Access_Specifier> <PropertyDataType> <PropertyName>
             {
               get { return <PropertyValue>; }
               [set { ViewState[“PropertyName”] = value; }]
             }
         −   Example:
               
                   public ListItemCollection FinalItems
                   {
                     get { return TargetList.Items; }
                   }
               
                   public Boolean AllowDuplicates
                   {
                     get { return (Boolean)ViewState[“allowDuplicates”]; }
                     set { ViewState[“allowDuplicates”] = value; }
                   }

    For readonly property we would have only get method

    For read+write property we would have both get & set methods

    Properties can be set declaratively (in attribute) or programmatically.
Demo 4 : Properies to User Control




Things to Note :     1. get & set syntax
                     2. ViewState has to be filled in manually for User Control
6. Define Methods of ASP.NET User Control


    User Control can have methods like ASP.NET Web Controls.

    To add a method to a User Control
        −   Syntax:
            <Access_Specifier> <MethodReturnDataType> <MethodName>
            {
              ...Method Definition...
            }
        −   Example:
              
                  protected void AddItem(ListItem li)
                  {
                    TargetList.Items.Add(li);
                  }
              
                  protected void ClearAll()
                  {
                    SourceList.Items.Clear();
                    TargetList.Items.Clear();
                  }
Demo 5 : Methods to User Control
Any Doubts ?

Weitere ähnliche Inhalte

Was ist angesagt?

Asp.net server control
Asp.net  server controlAsp.net  server control
Asp.net server controlSireesh K
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsRandy Connolly
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.netBhumivaghasiya
 
Presentation on asp.net controls
Presentation on asp.net controlsPresentation on asp.net controls
Presentation on asp.net controlsReshi Unen
 
Data controls ppt
Data controls pptData controls ppt
Data controls pptIblesoft
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1Neeraj Mathur
 
Parallelminds.asp.net with sp
Parallelminds.asp.net with spParallelminds.asp.net with sp
Parallelminds.asp.net with spparallelminder
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application Madhuri Kavade
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Vivek chan
 
Asp .net web form fundamentals
Asp .net web form fundamentalsAsp .net web form fundamentals
Asp .net web form fundamentalsGopal Ji Singh
 
Introduction To Asp.Net Ajax
Introduction To Asp.Net AjaxIntroduction To Asp.Net Ajax
Introduction To Asp.Net AjaxJeff Blankenburg
 
ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10Sisir Ghosh
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kitVidhi Patel
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 

Was ist angesagt? (20)

Asp.net server control
Asp.net  server controlAsp.net  server control
Asp.net server control
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server Controls
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.net
 
Presentation on asp.net controls
Presentation on asp.net controlsPresentation on asp.net controls
Presentation on asp.net controls
 
Data controls ppt
Data controls pptData controls ppt
Data controls ppt
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1
 
Parallelminds.asp.net with sp
Parallelminds.asp.net with spParallelminds.asp.net with sp
Parallelminds.asp.net with sp
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
Asp .net web form fundamentals
Asp .net web form fundamentalsAsp .net web form fundamentals
Asp .net web form fundamentals
 
Asp objects
Asp objectsAsp objects
Asp objects
 
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
 
Ajax
AjaxAjax
Ajax
 
Introduction To Asp.Net Ajax
Introduction To Asp.Net AjaxIntroduction To Asp.Net Ajax
Introduction To Asp.Net Ajax
 
Ajax and ASP.NET AJAX
Ajax and ASP.NET AJAXAjax and ASP.NET AJAX
Ajax and ASP.NET AJAX
 
Ch3 server controls
Ch3 server controlsCh3 server controls
Ch3 server controls
 
ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 

Ähnlich wie ASP.NET User Control Guide

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17Vivek chan
 
Simple Contact Us Plugin Development
Simple Contact Us Plugin DevelopmentSimple Contact Us Plugin Development
Simple Contact Us Plugin Developmentwpnepal
 
Asp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentChui-Wen Chiu
 
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...SPTechCon
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state managementpriya Nithya
 
Overview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company indiaOverview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company indiaJignesh Aakoliya
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questionsAkhil Mittal
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actionsEyal Vardi
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVCSunpawet Somsin
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 

Ähnlich wie ASP.NET User Control Guide (20)

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Visual studio 2008 asp net
Visual studio 2008 asp netVisual studio 2008 asp net
Visual studio 2008 asp net
 
12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
 
Simple Contact Us Plugin Development
Simple Contact Us Plugin DevelopmentSimple Contact Us Plugin Development
Simple Contact Us Plugin Development
 
Asp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component Development
 
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
ASP.NET - Web Programming
ASP.NET - Web ProgrammingASP.NET - Web Programming
ASP.NET - Web Programming
 
Overview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company indiaOverview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company india
 
ASP.NET Lecture 6
ASP.NET Lecture 6ASP.NET Lecture 6
ASP.NET Lecture 6
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
Asp.net
Asp.netAsp.net
Asp.net
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actions
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 

Kürzlich hochgeladen

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Kürzlich hochgeladen (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

ASP.NET User Control Guide

  • 1. 28-08-2009 DirectI ASP.NET User Controls Presenter: Viral Patel
  • 2. ASP.NET Controls  Types of Controls (1) HTML Server Controls (Traditional HTML Tags) (2) ASP.NET Server Controls (NEW ASP.NET Tags) (3) AJAX Server Controls (ASP.NET AJAX Controls) (4) Controls Created By Users
  • 3. Controls created by Users  ASP.NET User Control  ASP.NET Custom Control • Derived Custom Control • Composite Custom Control • Fully Custom Control
  • 4. ASP.NET User Controls 1. Prerequisites 2. User Control vs Web Page 3. Create ASP.NET User Control 4. Add User Control to an ASP.NET Web Page 5. Define Properties of ASP.NET User Control 6. Define Methods of ASP.NET User Control
  • 5. 1. Prerequisites  ASP.NET Web Page  Single Page Model  Code-Behind Page Model − Markup File - .aspx file (Design|Source) − Code File - .aspx.(vb|cs) file  ASPX file (Default.aspx) − <%@ Page Language=”C#” AutoEventWireUp=”true” CodeFile=”Default.aspx.cs” Inherits=”Default” %>
  • 6. Demo 1 : Create ASP.NET Web Page Note <%@ Page Directive
  • 7. 2.User Control vs Web Page ASP.NET Web Page ASP.NET User Control ASPX ASCX <%@ Page … %> <%@ Control … %> System.Web.UI.Page System.Web.UI.Control can run as stand-alone file cannot run as stand-alone file html, body, form html, body, form – not present
  • 8. 3.Create ASP.NET User Control • ASP.NET User Control – ASCX file (MyUserContol.ascx) • <%@ Control Language=”C#” AutoEventWireUp=”true” CodeFile=”MyUserControl.ascx.cs” Inherits=”MyUserControl” %> • MyUserControl inherits UserControl Class • Add ASP.NET Server Control(s) • Add ASP.NET Code for the Control(s)
  • 9. Demo 2 : Create ASP.NET User Control User Control Note <%@ Control Directive Web Page
  • 10. 4. Add User Control to an ASP.NET Web Page  User Control cannot run stand alone  It must be added to ASP.NET web page (aka host page) (.aspx)  STEP 1 : User Control needs to be registered to the host page − <%@ Register TagPrefix=”UC” TagName=”MyUserControl” Src=”~/Controls/MyUserControl.ascx” %>  STEP 2 : add the user control to host page − <TagPrefix:TagName ID=”UserControlID” runat=”server” /> − <UC:MyUserControl ID=”MyUserControl1” runat=”server” />  ALTERNATIVE: − Drag User Control and Drop to Design View of Web Page
  • 11. Demo 3 : Register & Add User Control to Web Page Registering User Control Things to Note : 1. <%@ Register Directive 2. TagPrefix, TagName, Src - Attributes Add User Control Note User Control Declaration
  • 12. 5. Define Properties of ASP.NET User Control  User Control can have properties like ASP.NET Web Controls.  To add a property to a User Control − Syntax: <Access_Specifier> <PropertyDataType> <PropertyName> { get { return <PropertyValue>; } [set { ViewState[“PropertyName”] = value; }] } − Example:  public ListItemCollection FinalItems { get { return TargetList.Items; } }  public Boolean AllowDuplicates { get { return (Boolean)ViewState[“allowDuplicates”]; } set { ViewState[“allowDuplicates”] = value; } }  For readonly property we would have only get method  For read+write property we would have both get & set methods  Properties can be set declaratively (in attribute) or programmatically.
  • 13. Demo 4 : Properies to User Control Things to Note : 1. get & set syntax 2. ViewState has to be filled in manually for User Control
  • 14. 6. Define Methods of ASP.NET User Control  User Control can have methods like ASP.NET Web Controls.  To add a method to a User Control − Syntax: <Access_Specifier> <MethodReturnDataType> <MethodName> { ...Method Definition... } − Example:  protected void AddItem(ListItem li) { TargetList.Items.Add(li); }  protected void ClearAll() { SourceList.Items.Clear(); TargetList.Items.Clear(); }
  • 15. Demo 5 : Methods to User Control